From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 0DA8DCD98E1 for ; Sat, 13 Jun 2026 21:48:40 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.1337557.1598873 (Exim 4.92) (envelope-from ) id 1wYWDA-0002SM-V6; Sat, 13 Jun 2026 21:48:16 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 1337557.1598873; Sat, 13 Jun 2026 21:48:16 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wYWDA-0002SE-R2; Sat, 13 Jun 2026 21:48:16 +0000 Received: by outflank-mailman (input) for mailman id 1337557; Sat, 13 Jun 2026 21:48:15 +0000 Received: from mx.expurgate.net ([194.145.224.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wYWD9-0001un-7N for xen-devel@lists.xenproject.org; Sat, 13 Jun 2026 21:48:15 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1wYWD8-00DVVl-KQ for xen-devel@lists.xenproject.org; Sat, 13 Jun 2026 23:48:14 +0200 Received: from [10.42.69.8] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6a2dcfd6-2eae-0a2a0a5409dd-0a2a4508ddf0-2 for ; Sat, 13 Jun 2026 23:48:14 +0200 Received: from [209.85.128.41] (helo=mail-wm1-f41.google.com) by tlsNG-c1860d.mxtls.expurgate.net with ESMTPS (eXpurgate 4.56.1) (envelope-from ) id 6a2dd01e-63b5-0a2a45080019-d1558029d029-3 for ; Sat, 13 Jun 2026 23:48:14 +0200 Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-4903d730b1fso26448045e9.2 for ; Sat, 13 Jun 2026 14:48:14 -0700 (PDT) Received: from localhost.localdomain (5.116.208.46.dyn.plus.net. [46.208.116.5]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-492202edf89sm113877065e9.1.2026.06.13.14.48.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 13 Jun 2026 14:48:13 -0700 (PDT) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" Authentication-Results: eu.smtp.expurgate.cloud; dkim=pass header.s=20251104 header.d=gmail.com header.i="@gmail.com" header.h="Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1781387294; x=1781992094; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=7om3qzciu7p1Hjf//vy/GA8MoXg6EtRp3ssDnE4xjCc=; b=U0W11znpk0ZdDBxXXs2NqOpVpghdC6+lHSJOEp5R5jc4OLigDqfhJusEEmdXl8Ml8N SEefZHJQx/q/Za9Sg8fBv+mugq5zH0eRNJN3COHKwGOsakl+6U4KvvFHwhxZhoHXOARL RrcyrXZXpmBpNahVEifR6GFtUKYLuisoYQh01cwYDJ/ft9zPbyXw3o1EC3o5TJaYjhsr tT+V465X58W2oXANUC8Ca6SwEqS8t7/yjBFhpTwfx2X5FInMgEkKpWAds03PULw1N542 fN/KWW6F8hAHrPirSMKxZOx+kGgFUf3b3+U+qY6ai6RpIJrIZXbmF8hzMXso16GvbFkU y4nA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781387294; x=1781992094; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=7om3qzciu7p1Hjf//vy/GA8MoXg6EtRp3ssDnE4xjCc=; b=qFYJjOLKHf8lQvZ0P+TwIMdsQOORDEtkTFbs1xizqXgbitM6a4qtInfCLM29d1KDY/ 3df4MeDZuiAgKjWH3SuTvhCOm3wTWIk4CB2hx/xoXWx5DbZA6mhz4mGjlJ6OoID1C/e0 iUm3Oznede0TYNHObPUooAORLYSvvl4OlIxzB3WSzDsmBLj3J71iY82Tn4INsM0DPFhD A6LEDo5diPiiEFrnTKjjwjBqa+tPE5PXfW/8vBEhnLZRiagujyRHlmHG9/DxfoNtlNm2 WauU9S4u1CG3Aw1p5MRhiDPL3ApWHmop/tZUNEVreEEacwC0WD3hKsgciF6kIkkmj0Xz MsYA== X-Gm-Message-State: AOJu0Yx4ZDFHdKZWGxq8r+9WB0c3Iz+KR+WAD4ucWheW8YZIBP8RXmBI PmbpEGRSzNiDYQ7rYahwnbdw5jSxTxmksR8J+oO+mTbrA5yaQcbhmLjG/ym6ulCRTnc= X-Gm-Gg: Acq92OFmDyl1M1aND/UYkrrk8uhPzjDp3SCrg4tggFf+2wr7hHawwVXUqIh2tGDrpRs vcwpHkgAJK4vH/zMrgQOPcMefVWxAWcwhHYJY9B5MMAJvVRHjc72NCsJdYeXBZ2GNhhD0XVaWUw Xdz0wXcUZxEG5PMcfh9TB5qSE+U2JFI+/bJSedkKB36DJ5v+s+NuIKuv7cfagTAauoCV8A3wR8d c3IoPOZ4AfIkLtg+pnwSC5mSlzsO6xVnKeHU6/sGrdRbz4ZXHIzkfOCFgz8luA06rILy0HGqpAT 0/fSkQbShmLeIWf5vaeuft0IP8tAZTWlJqcuHBF/h+x++arwZoVEdznn4PX28GE7JvrZVjGoGlh XDQfoz2PFupP1GVYB+aEhWRrsPeHh1zd1sW5pWsezUGvW0Zx4awbcwol++9m2j8smovCzJhjdP7 jiUrP4JbzHRiTnYF6say0g2bd0GWM6t9kKKx5RWtwfPZ+aSg+/mUid4bGeMxv/VLzsoE7gicbXR xr0j+B44XBdn876V/YXbMg6Ng== X-Received: by 2002:a05:600c:4709:b0:490:5057:f5f7 with SMTP id 5b1f17b1804b1-4922007472cmr57482665e9.11.1781387293908; Sat, 13 Jun 2026 14:48:13 -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 v5 06/16] libs/call: cache up to 4 pages in hypercall bounce buffers Date: Sat, 13 Jun 2026 22:47:39 +0100 Message-ID: <20260613214749.20620-7-frediano.ziglio@cloud.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260613214749.20620-1-frediano.ziglio@cloud.com> References: <20260613214749.20620-1-frediano.ziglio@cloud.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-purgate-ID: tlsNG-c1860d/1781387294-BF57FDB1-4A758F25/0/0 X-purgate-type: clean X-purgate-size: 5018 From: Edwin Török 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örök Signed-off-by: Frediano Ziglio -- Changes since v2: - change prefix in subject. Changes since v4: - fix off-by-one bug. --- tools/libs/call/buffer.c | 28 +++++++++++++++++----------- tools/libs/call/core.c | 3 ++- tools/libs/call/private.h | 8 +++++--- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/tools/libs/call/buffer.c b/tools/libs/call/buffer.c index 155e4f9d43..bde780c32c 100644 --- a/tools/libs/call/buffer.c +++ b/tools/libs/call/buffer.c @@ -56,13 +56,13 @@ static void *cache_alloc(xencall_handle *xcall, size_t nr_pages) if ( xcall->buffer_current_allocations > xcall->buffer_maximum_allocations ) xcall->buffer_maximum_allocations = xcall->buffer_current_allocations; - 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 = xcall->buffer_cache[--xcall->buffer_cache_nr]; + p = xcall->buffer_cache[nr_pages-1][--xcall->buffer_cache_nr[nr_pages-1]]; xcall->buffer_cache_hits++; } else @@ -84,10 +84,10 @@ static int cache_free(xencall_handle *xcall, void *p, size_t nr_pages) xcall->buffer_total_releases++; xcall->buffer_current_allocations--; - if ( nr_pages == 1 && - xcall->buffer_cache_nr < BUFFER_CACHE_SIZE ) + if ( nr_pages && nr_pages <= ARRAY_SIZE(xcall->buffer_cache) && + xcall->buffer_cache_nr[nr_pages-1] < BUFFER_CACHE_SIZE ) { - xcall->buffer_cache[xcall->buffer_cache_nr++] = p; + xcall->buffer_cache[nr_pages-1][xcall->buffer_cache_nr[nr_pages-1]++] = p; rc = 1; } @@ -108,17 +108,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 = 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); - while ( xcall->buffer_cache_nr > 0 ) + for ( unsigned i = 0; i < ARRAY_SIZE(xcall->buffer_cache_nr); ++i ) { - p = xcall->buffer_cache[--xcall->buffer_cache_nr]; - osdep_free_pages(xcall, p, 1); + while ( xcall->buffer_cache_nr[i] > 0 ) + { + p = xcall->buffer_cache[i][--xcall->buffer_cache_nr[i]]; + osdep_free_pages(xcall, p, i + 1); + } } 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 @@ */ #include +#include #include "private.h" @@ -44,7 +45,7 @@ xencall_handle *xencall_open(xentoollog_logger *logger, unsigned open_flags) xentoolcore__register_active_handle(&xcall->tc_ah); xcall->flags = open_flags; - xcall->buffer_cache_nr = 0; + memset(xcall->buffer_cache_nr, 0, sizeof(xcall->buffer_cache_nr)); xcall->buffer_total_allocations = 0; xcall->buffer_total_releases = 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; /* - * 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]; /* * Hypercall buffer statistics. All protected by the global -- 2.43.0