From nobody Thu Jul 23 21:11:33 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=1783975727; cv=none; d=zohomail.com; s=zohoarc; b=VtlNy5Q0E5riy/Yg2D25sWT1xcqMf+dhOocKSFHZ2wtD6+HBmcbLGSA3AmIDHbm8ElScFDhqvh0QqF9Q4qpIDNGzWJO82e9CyoHjVAJmD+AJgVQru06WeYUQ7xh51/s1/wgXYhjomfzohbZ++OBCGrP97XvplYAh56p/0XbL+tE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1783975727; 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=wimWBVOYQqwPaXI8zWtV5Ue4b9r9c1tb/eSNhf7mAu8=; b=XkNazXR2w1+zNcT4lpbbdfwKK7LHR8Z15soDVjJB7KFcSDuTE9/4bG/pLyPi5Dp+HzKliYycypERMIEWW/vPh8QtPTzHqhDK/E1FVqZTuIw/ImyinphkDGvuidnEroNoTNQ6ARO1FpfcnjiN5DXEwNoJn8cOJ1zxT6vPDdOUPvI= 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 1783975727014250.64746812748115; Mon, 13 Jul 2026 13:48:47 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1361929.1613956 (Exim 4.92) (envelope-from ) id 1wjNZV-0005My-CA; Mon, 13 Jul 2026 20:48:13 +0000 Received: by outflank-mailman (output) from mailman id 1361929.1613956; Mon, 13 Jul 2026 20:48:13 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wjNZV-0005Mr-7o; Mon, 13 Jul 2026 20:48:13 +0000 Received: by outflank-mailman (input) for mailman id 1361929; Mon, 13 Jul 2026 20:48:12 +0000 Received: from mx.expurgate.net ([195.190.135.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wjNZT-00059x-Rr for xen-devel@lists.xenproject.org; Mon, 13 Jul 2026 20:48:11 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1wjNZS-00FW6X-IN for xen-devel@lists.xenproject.org; Mon, 13 Jul 2026 22:48:10 +0200 Received: from [10.42.69.12] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6a554ed4-5cb7-0a2a0a5109dd-0a2a450cb27e-30 for ; Mon, 13 Jul 2026 22:48:10 +0200 Received: from [209.85.128.53] (helo=mail-wm1-f53.google.com) by tlsNG-d25034.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.2) (envelope-from ) id 6a554f08-e897-0a2a450c0019-d1558035b036-3 for ; Mon, 13 Jul 2026 22:48:09 +0200 Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-493b1710405so19353945e9.2 for ; Mon, 13 Jul 2026 13:48:09 -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-49508724786sm23924255e9.3.2026.07.13.13.48.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Jul 2026 13:48:07 -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=1783975688; x=1784580488; 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=wimWBVOYQqwPaXI8zWtV5Ue4b9r9c1tb/eSNhf7mAu8=; b=p5bS73IUQcRSU3oHBOSWwISXFxYSHtz5P+sL1XWoivaFnyl0oOUmkqdQwhlMMfUohH uUr7bO+u4Uqme/Se4P/DmSctrAjhaJ801F3wE7GAUM6dXjCKWdroMGwLvfUQz8fl6ni2 AGoQskVCyvpRSE+3arVoIGVIAymGCHAKNbe5Q0mY6BRr1JqU1cOsunITyqBNAo65lfU/ rk+BeTeSTajYob2KCQ2t4tEtket0hK1KHKRkWl8GC7jMwMGhWUgOCjq7EwQKOUm51WaN nspvihu1QobN2nMbM0ohaT6EYZAnE/oBRijjH+fsDRq8qH7inL6t7wsufZU/eLOb15gd 038g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783975688; x=1784580488; 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=wimWBVOYQqwPaXI8zWtV5Ue4b9r9c1tb/eSNhf7mAu8=; b=RaP8xZqkHEBH46HZ/moFISpuIpGHIyMO4tdVZFURHLHZPRttW/AhoXyFUauwDC2J+5 6bfqe+2+0GdpJREzqfbapjH520aiTVdmPZYm2y7FPBc/6eeo6HY2OAVp9HoSvkED8h+8 oofPpLWSW+wFob/V6EPHtZxip93dicCx5kYxqTNw4c48a1E3iyfMqU8Ys30uZwmAgeQ5 FrrAIW6lHPH+cJF0KBL2aoP2UZvBckv/gmAokJZpUpfPXXTkSekigBIysfzqnZ9RxFrw KcMKAJ5USm0jmDRv5MasHRsm579BSL8ebmFGx5EFn/VEqCTaarJ8NjYDJVWddxjf1/zw DUdw== X-Gm-Message-State: AOJu0YxPJksb2iDYdLIc3ivwL7rCBUz4FBkREa+OKlDy25EOALKFqNoe hwRadVmUixrSm8x6hsDN5ObAiGrGfWCLnteFyrjhaHHEJcNV7/3LH81HwFxttX1bNCY= X-Gm-Gg: AfdE7clccAQILj60TqSiu+pz9SidJmvdb2ZeGNx3EwHGDVb9lySNXOPhLNGYO9s5IKl JnX/deaPIKAokn4JOgj2Qwu6F3f89rZ280/p4QhiFNoMmCo2ZPe5Ws7fap4Nf1RQOHZqvu5UtNU W7GcRFb/EWgLjFkJnhpOttd3CMygHsnWbIucMdCdtXww1cptCSGF+3/rJblflfMHJzoGipvw1v6 8/yZkSI+I0Bw1JfjJ2swdtzj67z7Qn/jbu7qZY5W7A4VGJFwxUGYgjo7tGFulMZDt3NDVe1IYoS TJWUzvvZhI8BPHJ3EnjQLws7Tym7GaDXHiSwP0yXGtiKapHx6lDfy6HkqIxKkHTrcStOaaLRclI W/iOBkfZ0So+SkUjCwjnt+ObpOHaR0cv9Q0q6Bsc19kH18OpPCxG17ePkrmTU1gP1XuKldRx6yt sgqwGuldbb5YkN7HaWj/pmPjUF2TmL5B1J3V7ysFeQdapc60vsSabbUyjpGc5onzv0+ybl0Um3Q owe31zTDcFsuUoZDck= X-Received: by 2002:a05:600c:8119:b0:493:f744:e7d5 with SMTP id 5b1f17b1804b1-49518324f47mr8276255e9.38.1783975688441; Mon, 13 Jul 2026 13:48:08 -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 v9 1/4] libs/call: cache up to 4 pages in hypercall bounce buffers Date: Mon, 13 Jul 2026 21:47:59 +0100 Message-ID: <20260713204802.105115-2-frediano.ziglio@citrix.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260713204802.105115-1-frediano.ziglio@citrix.com> References: <20260713204802.105115-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-d25034/1783975689-452FA6B2-A076A490/0/0 X-purgate-type: clean X-purgate-size: 5706 X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1783975729010158500 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". Changes since v8: - added Reviewed-by. --- 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:11:33 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=1783975717; cv=none; d=zohomail.com; s=zohoarc; b=kqSN2/hFwCE9MuUi4q2suMPc8owFjFf2qDzoCczWk+BH+QDkzqJ0DfBVKg7E/eFv+llSzgJb7pAO9/HhqbyG1AAAQo3IxWKwecSanXwes9pljNEVCMI0gRop3l/n9jHFR7x6Xgta0FQ6tX9TtDavZSqkQW2bKQPL8Km7/XIcOrQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1783975717; 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=GVzljfbFKFoNV9C92LIEgW8vPbCFaLYksP7zU4eJtPg=; b=WV8ylb4d2wLZCQu8GuSrqBefxBTDMYqwRyhjkS6Wym/0O+LTorD9ALTfyDB7IJZzauXZcFkN478RhSv6VonTTOWvSD1tvXJ57A502lVwBqIqQp68TjSPXxBZ1/aQ7NpRqlMzdC5QyovbgLm4vnMbDOyv/wkvkB1uD6um5usIuVQ= 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 1783975717266645.3453021518136; Mon, 13 Jul 2026 13:48:37 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1361928.1613960 (Exim 4.92) (envelope-from ) id 1wjNZV-0005PP-I1; Mon, 13 Jul 2026 20:48:13 +0000 Received: by outflank-mailman (output) from mailman id 1361928.1613960; Mon, 13 Jul 2026 20:48:13 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wjNZV-0005Od-F4; Mon, 13 Jul 2026 20:48:13 +0000 Received: by outflank-mailman (input) for mailman id 1361928; Mon, 13 Jul 2026 20:48:12 +0000 Received: from mx.expurgate.net ([195.190.135.20]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wjNZT-0005Ch-RS for xen-devel@lists.xenproject.org; Mon, 13 Jul 2026 20:48:11 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1wjNZS-00EnAW-RK for xen-devel@lists.xenproject.org; Mon, 13 Jul 2026 22:48:10 +0200 Received: from [10.42.69.4] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6a554ec9-2eae-0a2a0a5409dd-0a2a45048844-44 for ; Mon, 13 Jul 2026 22:48:10 +0200 Received: from [209.85.128.44] (helo=mail-wm1-f44.google.com) by tlsNG-ebf023.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.2) (envelope-from ) id 6a554f0a-b1e5-0a2a45040019-d155802ccc2c-3 for ; Mon, 13 Jul 2026 22:48:10 +0200 Received: by mail-wm1-f44.google.com with SMTP id 5b1f17b1804b1-493ec555a26so23344065e9.0 for ; Mon, 13 Jul 2026 13:48:10 -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-49508724786sm23924255e9.3.2026.07.13.13.48.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Jul 2026 13:48:09 -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=1783975690; x=1784580490; 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=GVzljfbFKFoNV9C92LIEgW8vPbCFaLYksP7zU4eJtPg=; b=Lfpyq0fGX2JbH3BvmU6VFdcBYCw1eUxmF9Kzk2HN1OmikL9mOzmtBCR0ExXFEfujYs jBFpYt8VXWI9HqZkp651BREYvfKWIWb1eJk2NS+U8RWdCjXuD1xrNpM5ZCykrvclC0LB 8dVAGqhygtiJ2DDJFD2Ds5xUMi2KJRTMJA9KIpYKy6d4RNY7jsXSF/pLfRYzJnuRGp/d MJBSwRN4yqTLCgrxXg/zRLy1Iogr7KSNsYHG/N0vMcuoVYuIOOR0Bk0lxvOh7+CzS9Jf MfBl00jH7gS2AX1lorr4HO/8kVceP1bjwczl3wztCVQaYpr7FAuhPOwWWrl8hjfECOno lLIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783975690; x=1784580490; 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=GVzljfbFKFoNV9C92LIEgW8vPbCFaLYksP7zU4eJtPg=; b=emsP145JUMrID6oFJP8xow1dkLbPbPGUQ7WSHJnCpkbdDCK/6ROQJFOkkxP255m5lC Ocrb4qIjbEpVBQ8X3TC4dxEGpz6zDIK9thchWYuEGQ4Ct64ZwuS7RHvynYRTp/dmfKFe q3PyWHYZ8tcjO/MrB5nQAA5e5qiFFE42IhHy8eQvk/p1+zlYDuTSbnl2n6StpmBxKaBS 7VtvlxYVYebuQlJo/o2pUswF8Uv0vJDSrtJvPd1EcKFjFVwjAoSZMYDa8vOoYWKmkSjB gMrniLHeGCEw/8ZGcWoc159zN9ZLbdtRBbXdJ4mKAn1avtZDOEcktGVX6ksIUQhtkhPf 4HLw== X-Gm-Message-State: AOJu0YyUvlwfE8KzvYe5fML2kUtTMNuVc6jt1FUef6YU01NahzGkxXUW qEd44Mn1irypBRDEReQjg+SDHVypQeI2kj5g1OoLcrf8Qw5l1pLC7Cn1F8ladf18WJaABw== X-Gm-Gg: AfdE7cnmrsunqDpp8QVFHV9A7Pdc14PpT2BYQxj6DFggTwro3HPJH963tTB35cB4NbP dTLu8b3dC41hKxjCjR3Z9Z0Q2AEXLxfkQ8mFnGCURXLxPoXjJIutEAw2LEON/DrVmKHeCkIVwMM aSdiiwjjYTxCA2/JGrxorerXw5h6NbdJ9Fmw8CLI5RDCw1SPaeKM89SjTABrhP2QBYpfkXw/hRR VSeJ3vLWGI2K5UKp58Dku5VUMBUaT0YSyj3mdlTRnZLgAwMK9kJwaQs8aETwaV5Sxo5cbwWcfrh W5yBlkP8/xGoZywZM+4yPvx+kYrqtwNzfV/IAyufr52skpAh6RQuhYYlN4xVbQWGkqp8m+o+7Y8 ayXsk+V/v3lpk7cueVTJUg3i7vzZtknG5bCbm3DCTsBoWWQOeykxopXendCiS1qY3HzHhnCfL8d OFH5yhN8JFGO1ufYcKM4XZ4T+e45jdu9A8E716s3IQ3es6a3yiKSaKdyRhy55pAuCwuaTcsq3mM +IkFzj7EVTqA5CW507NKYBEFahv7Q== X-Received: by 2002:a05:600c:3d8b:b0:48a:906b:14ca with SMTP id 5b1f17b1804b1-493f8818e5dmr116948595e9.20.1783975690107; Mon, 13 Jul 2026 13:48:10 -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 v9 2/4] libs/guest: move batch_pfns into a separate structure Date: Mon, 13 Jul 2026 21:48:00 +0100 Message-ID: <20260713204802.105115-3-frediano.ziglio@citrix.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260713204802.105115-1-frediano.ziglio@citrix.com> References: <20260713204802.105115-1-frediano.ziglio@citrix.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-purgate-ID: tlsNG-ebf023/1783975690-75E53FE7-6214D1A6/0/0 X-purgate-type: clean X-purgate-size: 6125 X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1783975719089158500 Content-Type: text/plain; charset="utf-8" Preparation for a followup patch "libs/guest: allocate various migration arrays just once". 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. Changes since v8: - remove useless check; - added Reviewed-by. --- tools/libs/guest/xg_sr_common.h | 5 ++++- tools/libs/guest/xg_sr_save.c | 28 ++++++++++++++-------------- 2 files changed, 18 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..22348db445 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,7 @@ static int write_batch(struct xc_sr_context *ctx) }; =20 assert(nr_pfns !=3D 0); + assert(nr_pfns <=3D MAX_BATCH_SIZE); =20 /* Mfns of the batch pfns. */ mfns =3D malloc(nr_pfns * sizeof(*mfns)); @@ -141,13 +143,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 +194,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 +208,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 +236,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 +275,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 +294,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 +785,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 +819,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:11:33 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=1783975731; cv=none; d=zohomail.com; s=zohoarc; b=HmTn9z2TT+FwwJILV19+MUg1c/TDM5ewFqSXfE/MBduLJ7FI5g7cZ3ZfMq/Kmasfag5h+d1FPEB/3CJHw7aLwAqX6qqjHcpBQs3CXgwWl/qPUp1sbe+3ocdOprpPSxxeqiBy0IVXoHJa9ohtsXMDdlJ9ODlc4nAH4qwZkRiGecc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1783975731; 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=qGsmWUifmdUWchWcdJY2/HqBgvlxTOt3fS0Wzpm+gj8=; b=TKq6v9VKgWl7U8sNVk0UG8Q6dyntBCChAq3FEkZlstjEjCFwecoHUmdrfP/Q0Qjgn68fu3Mq7ZlA9qE1SXume8JB0siJmAbei7l45V//oIXM57zQmLetnPELLB6nUwDi4oSYmaDWChiz7iVoN6P3AoQopOpyQ1yNdCAbDeunGLw= 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 1783975731285353.56641111332635; Mon, 13 Jul 2026 13:48:51 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1361930.1613968 (Exim 4.92) (envelope-from ) id 1wjNZW-0005Z8-2a; Mon, 13 Jul 2026 20:48:14 +0000 Received: by outflank-mailman (output) from mailman id 1361930.1613968; Mon, 13 Jul 2026 20:48:14 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wjNZV-0005X5-Rt; Mon, 13 Jul 2026 20:48:13 +0000 Received: by outflank-mailman (input) for mailman id 1361930; Mon, 13 Jul 2026 20:48:12 +0000 Received: from mx.expurgate.net ([195.190.135.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wjNZU-0005Mj-JH for xen-devel@lists.xenproject.org; Mon, 13 Jul 2026 20:48:12 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1wjNZU-00FW63-04 for xen-devel@lists.xenproject.org; Mon, 13 Jul 2026 22:48:12 +0200 Received: from [10.42.69.3] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6a554f02-bab6-0a2a0a5309dd-0a2a4503e5b2-10 for ; Mon, 13 Jul 2026 22:48:11 +0200 Received: from [209.85.128.44] (helo=mail-wm1-f44.google.com) by tlsNG-33051d.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.2) (envelope-from ) id 6a554f0b-f2d2-0a2a45030019-d155802cd177-3 for ; Mon, 13 Jul 2026 22:48:11 +0200 Received: by mail-wm1-f44.google.com with SMTP id 5b1f17b1804b1-493f60208a5so31038145e9.3 for ; Mon, 13 Jul 2026 13:48:11 -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-49508724786sm23924255e9.3.2026.07.13.13.48.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Jul 2026 13:48:10 -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=1783975691; x=1784580491; 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=qGsmWUifmdUWchWcdJY2/HqBgvlxTOt3fS0Wzpm+gj8=; b=I/9ZbTyCXO8ypfQgF8+ZADatNTI80pexwrz9iWsUXvLO2JPWOLzzj0Iw93eDhDdkmn iEs336MTe2dbkNUZBGluEK7oZCcsHcujh/ATf7EVRtFnLxbC8uvulKD2rzBn5Nl2Nbal CvxiewbhrkH1phcHNPrkZV1oE3SmUav7uZgEXwXfXBod1ockzeOghFQqfx+JWsojbTv9 qr/P/bwdWlHs6+Sfr+FvtTQLNFs2pjmx5ZUItCRBKvjLsVnppQLr9g/aNvuCBEgKj8Qo BQpUk0Jtu+wiWbapVU+zPR1O1TwyWisGXqCjVVXJ9VyWGkoWri9aeOwiAdVegOBtkynE gCGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783975691; x=1784580491; 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=qGsmWUifmdUWchWcdJY2/HqBgvlxTOt3fS0Wzpm+gj8=; b=oLB4Eg/XbHZEmrEWCD5NOe3qqXpemZeWi8N7JFvhmv19mRTMFVxYnJal2aA2brCt0a hTxu2fxXMmGzU3kTCtYdQNn5bSJowCs+wP9oa6imrGgGh/8/xH5IZ+3aLXJlTPUv5794 v0ttopHPdQ7SSiDeTZcLhe6xOKLuQPEFK7aoXcrCfq5i2Jm/bMx0RaEe7+lzESoNk3Ar oxReOR6fXklFmaMasrPNvOHP9xuO+rxspnvFUS44bnB321/LnmTySslN7KoL5aOcu7Zt Fsbtvvd0NGPUJm3d4DXYlfOMiF9qqEYNIr6Q/6eSjLseNoSwEa0qsmNOlK+nyaoz9avN qWgQ== X-Gm-Message-State: AOJu0YxvHwALQ71Pi0f9xTNBBqry07Wm1IuA40pGemVQ3nEX+Yjz+vA7 EB5v57A9JRuheUa7dTy7684X6eHGhcBAhTc8Y02qVJz5UXuK8F9vCfbgOWTHsm5vOX84Wg== X-Gm-Gg: AfdE7cm4vWMU7obTIxkxbNazKtWJAOP7HDD1xqbMLU8E/0stMUODJVBO5KK2hTZFybh Tedrn/xxuOU/t2kqqVYe/4J7tq6QfzQ3zac81TOUtJfcE4pquxCsE1hmtX5flTJfU91UCEWlaWc IgbmzVqDFahnLNEA3WEAWxxpBDS8mM2qalTmQ2Kn4pUHQpY1aDLPwxeoKETNMqEKGU6yjQ805y1 wMVNKwrL0kn7wwvPJTpVJmqJEBfkvHwkZmJvFXhSYZXHVy7MYCdH5wYHekGQjrECDvkyTPEqX+X /j70QNvX/yhF7uawQQ3REj+v2rZ8iuTV78AJU8ygf1e53conckIuEKLHYBsjbSf71+pYzPYVGIv bjtr6wzs1KdcaUbctsr0/Z7Be2uNfWpOZoF/Uf6M8f0Njh8LeiwMNV1zBDVHb/wr0lRhALRiLTI K0ozPGrjlfZoxLL6lO+88BZRm2iERoZXCFRdFhg/O3y3SbWnZD3xFtoeJbF9l7FZEpO5Q0kT55B sBqt9XQmTKxt0OyI7k= X-Received: by 2002:a05:600c:468f:b0:493:df1d:7488 with SMTP id 5b1f17b1804b1-493f87e5b4cmr116516855e9.16.1783975691233; Mon, 13 Jul 2026 13:48:11 -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 v9 3/4] libs/guest: allocate various migration arrays just once Date: Mon, 13 Jul 2026 21:48:01 +0100 Message-ID: <20260713204802.105115-4-frediano.ziglio@citrix.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260713204802.105115-1-frediano.ziglio@citrix.com> References: <20260713204802.105115-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-33051d/1783975691-C0B5DCF4-400B2EB9/0/0 X-purgate-type: clean X-purgate-size: 4798 X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1783975733180158500 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 Reviewed-by: Anthony PERARD --- 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. Changes since v8: - remove useless memset; - initialize variables while declaring them. --- tools/libs/guest/xg_sr_common.h | 6 +++++ tools/libs/guest/xg_sr_save.c | 45 ++++++++++++--------------------- 2 files changed, 22 insertions(+), 29 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 22348db445..6a77e33a47 100644 --- a/tools/libs/guest/xg_sr_save.c +++ b/tools/libs/guest/xg_sr_save.c @@ -86,15 +86,12 @@ static int write_checkpoint_record(struct xc_sr_context= *ctx) static int write_batch(struct xc_sr_context *ctx) { xc_interface *xch =3D ctx->xch; - xen_pfn_t *mfns =3D NULL, *types =3D NULL; void *guest_mapping =3D NULL; - void **local_pages =3D NULL; - int *errors =3D NULL, rc =3D -1; + int rc =3D -1; unsigned int i, p, nr_pages =3D 0, nr_pages_mapped =3D 0; unsigned int nr_pfns =3D ctx->save.nr_batch_pfns; void *page, *orig_page; - uint64_t *rec_pfns =3D NULL; - struct iovec *iov =3D NULL; int iovcnt =3D 0; + int iovcnt =3D 0; xen_pfn_t *const batch_pfns =3D ctx->save.buffers->batch_pfns; struct { struct xc_sr_rhdr rec; @@ -110,28 +107,21 @@ static int write_batch(struct xc_sr_context *ctx) }, }; =20 - assert(nr_pfns !=3D 0); - assert(nr_pfns <=3D MAX_BATCH_SIZE); - /* Mfns of the batch pfns. */ - mfns =3D malloc(nr_pfns * sizeof(*mfns)); + xen_pfn_t *const mfns =3D ctx->save.buffers->mfns; /* Types of the batch pfns. */ - types =3D malloc(nr_pfns * sizeof(*types)); + xen_pfn_t *const types =3D ctx->save.buffers->types; /* Errors from attempting to map the gfns. */ - errors =3D malloc(nr_pfns * sizeof(*errors)); + int *const errors =3D ctx->save.buffers->errors; /* Pointers to locally allocated pages. Need freeing. */ - local_pages =3D calloc(nr_pfns, sizeof(*local_pages)); + void **const local_pages =3D ctx->save.buffers->local_pages; /* iovec[] for writev(). */ - iov =3D malloc((nr_pfns + 2) * sizeof(*iov)); + struct iovec *const iov =3D ctx->save.buffers->iov; /* page_data record PFNs list */ - rec_pfns =3D malloc(nr_pfns * sizeof(*rec_pfns)); + uint64_t *const rec_pfns =3D ctx->save.buffers->rec_pfns; =20 - if ( !mfns || !types || !errors || !local_pages || !iov || !rec_pfns ) - { - ERROR("Unable to allocate arrays for a batch of %u pages", - nr_pfns); - goto err; - } + assert(nr_pfns !=3D 0); + assert(nr_pfns <=3D MAX_BATCH_SIZE); =20 iov[0].iov_base =3D &hdrs; iov[0].iov_len =3D sizeof(hdrs); @@ -249,14 +239,11 @@ static int write_batch(struct xc_sr_context *ctx) err: if ( guest_mapping ) xenforeignmemory_unmap(xch->fmem, guest_mapping, nr_pages_mapped); - for ( i =3D 0; local_pages && i < nr_pfns; ++i ) + for ( i =3D 0; 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; } @@ -790,8 +777,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:11:33 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=1783975720; cv=none; d=zohomail.com; s=zohoarc; b=ePip5AYzOB8E1GExgasLqPTOHqL8fg53LdgLrYrFGbImgayaBZIVFEW+Z1DbuxUoMpjG33eHqCZsJo9VoaRhmy3lAqa0xhkCsjsS+9NXgt1Qta9ya4/9kL4rNDXynkDEynm9LsmkvZQa9aqxhnpclGFPlcyNvFdO5vvRf2cLKC0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1783975720; 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=ch0w6GZSKmn/pD177LTY+dgX+ATbp49+AP+ziJtpRo4=; b=Ggzo/fqLRE0bJWxYl3UJCqVQADVN0aYIGo/a1GEL7UxoEW+09/tDAeCjvbnFDf7RCEGk32zLGB7uknMBLO2JTiqjkHK/lP/obMP1Ku8o3E/iqEmfimRNWb9qC4ltsG4ktjmKFSpSd1auOXa/8dkiNdJXbSfeD8M79U/KEr4W0oM= 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 1783975720110936.112455631518; Mon, 13 Jul 2026 13:48:40 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1361931.1613984 (Exim 4.92) (envelope-from ) id 1wjNZX-000603-95; Mon, 13 Jul 2026 20:48:15 +0000 Received: by outflank-mailman (output) from mailman id 1361931.1613984; Mon, 13 Jul 2026 20:48:15 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wjNZX-0005zo-3u; Mon, 13 Jul 2026 20:48:15 +0000 Received: by outflank-mailman (input) for mailman id 1361931; Mon, 13 Jul 2026 20:48:13 +0000 Received: from mx.expurgate.net ([195.190.135.20]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wjNZV-0005P5-LY for xen-devel@lists.xenproject.org; Mon, 13 Jul 2026 20:48:13 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1wjNZV-00EnAW-2c for xen-devel@lists.xenproject.org; Mon, 13 Jul 2026 22:48:13 +0200 Received: from [10.42.69.9] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6a554ea5-2eae-0a2a0a5409dd-0a2a4509816e-38 for ; Mon, 13 Jul 2026 22:48:13 +0200 Received: from [209.85.128.45] (helo=mail-wm1-f45.google.com) by tlsNG-bad1c0.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.2) (envelope-from ) id 6a554f0c-b440-0a2a45090019-d155802dcc88-3 for ; Mon, 13 Jul 2026 22:48:13 +0200 Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-493ec555a26so23344225e9.0 for ; Mon, 13 Jul 2026 13:48:12 -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-49508724786sm23924255e9.3.2026.07.13.13.48.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Jul 2026 13:48:11 -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=1783975692; x=1784580492; 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=ch0w6GZSKmn/pD177LTY+dgX+ATbp49+AP+ziJtpRo4=; b=DoFUHvUv1Yz8IcnR9ua60g+CflresRB9sgbfkNas20lzhd9mkT9Xn2VEOJUIiOlFzH abMhEZNJ2W7oSJJJ2FjEKYFpcSoYGN1JWRtk3ajti3hrKgCHDm/gMAVd+saMkj6WtDhF lMj/V6ysFtCmaNNb8SWgPpzFenxeo3jdXAsX3QqavyiFminLkq9JMQ4Eqj3oP7odcKqV lLK9+owhZ5BZ4QBqHLA+NHc6kuTy7i8pKdunok49i+kKEKOyn0JTa+EoGm1Yn3Ku669C xizzUyw2sP9/6xFVadVJ+mXxV9KvOqVlpCv9ZzkZkgXwnGnc03a60aFAMWEgRKPF4NPJ rbVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783975692; x=1784580492; 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=ch0w6GZSKmn/pD177LTY+dgX+ATbp49+AP+ziJtpRo4=; b=OPMWRTYd/339HaxE/uvA6uTj0WyBPFMo/5GqxWG29oSZ8wBDSdWm+6ZCZUA8Uvtu+B UVRVz7qcwBqGjrV+EteSnZRGrvDA9Zx+z3Uu7nRQV2JVL3lla7wyfrIpEJhk5K4kuPJK XrFWvJ/Ja/t/hveO2RR78Dz3vu/xWLYEjIBXBBvQijbIlidOerL4hmIgWLFajwbYTLpJ Vjw+fuYemNFjj8+5Mzg7A3NZJZK+gHSRPUVWsCw/ljy70XF2H/+7VZ9fJ8Lzp/dI3dxd fUYXRrb8hVqCfdHGDiDixFw8eRNZSlwqynRrpGma1fwGGvXeu2kLrimwd5Fi9ykw8lMU 15Hw== X-Gm-Message-State: AOJu0Yy6du0chR8NcYjCQt67QhJZuaMKgRkZazHevD+EZvskygnaf4C1 yJ9Rg8vjj427VRp5rGSqRA9PmiMapGvlChOWd0XKEuiyPjSzbtkNNmClB7vXmqbzbjLrLQ== X-Gm-Gg: AfdE7cmUpYizt1XYz5Eu2SlTKiHQscCi5Uaz1/yx+ji7rRpLiNsMUGxO6aP5zKv9xwP Sg8fjr2FCRD/HUF5QJFj8f86/gn0rsnT/kI1VhimAM1vZlyHe6GsRSvn02V4uJZjYMSM2U8IW00 D/5D0cKpCxAeB9fXuXxTDBw+vHIIFpzH6+OD+a6LwQOeAtdyUJ3lXA9KBJv0KldLeeIWDOfa/Up Qak9STBV5W0vzIjzjMBjdW5LrbZTv/KjMibY92hfFJr8fDImOqQ9Yhs5i+zh2Dv7aNSa2p041NK Wh0KaT60mD/PR5gN8AA2FhmLTn0ywbacXorXoRxrvi33fEtR/J1AUYKYkwunveTTxt379jIqc8S SFiTvI8hGD/3KQ5aFECl7a5nAIbESwjh6OGHeRWRskHPA3XwI5qJYtHdowvWTNiRGO8bRTKTMrf TNlNUAfr9uqxd5DRo/9FDVJkxV18+NXbhyd+OjM84QW38/BcxU/NwxDGGruSMEVJVReqpLMxzRj p7uG1d8yW5Z5z8H7Ss= X-Received: by 2002:a05:600c:1f91:b0:493:e79e:daa6 with SMTP id 5b1f17b1804b1-493f8826e69mr103268785e9.33.1783975692202; Mon, 13 Jul 2026 13:48:12 -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 v9 4/4] libs/guest: use Valgrind to detect various buffer overflows Date: Mon, 13 Jul 2026 21:48:02 +0100 Message-ID: <20260713204802.105115-5-frediano.ziglio@citrix.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260713204802.105115-1-frediano.ziglio@citrix.com> References: <20260713204802.105115-1-frediano.ziglio@citrix.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-purgate-ID: tlsNG-bad1c0/1783975693-55971A0B-242463AA/0/0 X-purgate-type: clean X-purgate-size: 3522 X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1783975720940158500 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 | 13 +++++++++++++ 3 files changed, 29 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 6a77e33a47..25561e369f 100644 --- a/tools/libs/guest/xg_sr_save.c +++ b/tools/libs/guest/xg_sr_save.c @@ -123,6 +123,11 @@ static int write_batch(struct xc_sr_context *ctx) assert(nr_pfns !=3D 0); assert(nr_pfns <=3D MAX_BATCH_SIZE); =20 + MEM_UNDEFINED_INIT(ctx->save.buffers->mfns); + MEM_UNDEFINED_INIT(ctx->save.buffers->types); + MEM_UNDEFINED_INIT(ctx->save.buffers->iov); + MEM_UNDEFINED_INIT(ctx->save.buffers->rec_pfns); + iov[0].iov_base =3D &hdrs; iov[0].iov_len =3D sizeof(hdrs); =20 @@ -783,6 +788,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