From nobody Sun Feb 8 05:53:39 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 030DFC83F15 for ; Tue, 29 Aug 2023 01:56:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235025AbjH2Bzz (ORCPT ); Mon, 28 Aug 2023 21:55:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40096 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235100AbjH2Bzc (ORCPT ); Mon, 28 Aug 2023 21:55:32 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 633A0184 for ; Mon, 28 Aug 2023 18:54:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1693274076; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=YeQOl0ZS3saHbXQy2AG80w7b5li3nGbIMrsS1CssN30=; b=Y8Mmx6Sji2P3cOwXXdMOkJuXfbKqT88qo28z4UFHV9UR/hRTFASZxDfEJgnEBbmYNxebVi 6laz+zKcoIqDZLf7pakQE1ASOk9JAtBrrEbhsbAZFlGHEKcxgJbew3sNSVMKHzA/s292NE 9Vs4sB6BEi8xV0Jk4sW+5nVE8QhO2DE= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-376-7kD-3s0KMKmslGvURDC0qg-1; Mon, 28 Aug 2023 21:54:34 -0400 X-MC-Unique: 7kD-3s0KMKmslGvURDC0qg-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 6FBFA101A528; Tue, 29 Aug 2023 01:54:34 +0000 (UTC) Received: from gshan.redhat.com (unknown [10.64.136.7]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 5126740C2063; Tue, 29 Aug 2023 01:54:31 +0000 (UTC) From: Gavin Shan To: virtualization@lists.linux-foundation.org Cc: linux-kernel@vger.kernel.org, david@redhat.com, jasowang@redhat.com, mst@redhat.com, xuanzhuo@linux.alibaba.com, shan.gavin@gmail.com Subject: [PATCH] virtio_balloon: Fix endless deflation and inflation on arm64 Date: Tue, 29 Aug 2023 11:54:21 +1000 Message-ID: <20230829015421.920511-1-gshan@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The deflation request to the target, which isn't unaligned to the guest page size causes endless deflation and inflation actions. For example, we receive the flooding QMP events for the changes on memory balloon's size after a deflation request to the unaligned target is sent for the ARM64 guest, where we have 64KB base page size. /home/gavin/sandbox/qemu.main/build/qemu-system-aarch64 \ -accel kvm -machine virt,gic-version=3Dhost -cpu host \ -smp maxcpus=3D8,cpus=3D8,sockets=3D2,clusters=3D2,cores=3D2,threads=3D1 \ -m 1024M,slots=3D16,maxmem=3D64G \ -object memory-backend-ram,id=3Dmem0,size=3D512M \ -object memory-backend-ram,id=3Dmem1,size=3D512M \ -numa node,nodeid=3D0,memdev=3Dmem0,cpus=3D0-3 \ -numa node,nodeid=3D1,memdev=3Dmem1,cpus=3D4-7 \ : \ -device virtio-balloon-pci,id=3Dballoon0,bus=3Dpcie.10 { "execute" : "balloon", "arguments": { "value" : 1073672192 } } {"return": {}} {"timestamp": {"seconds": 1693272173, "microseconds": 88667}, \ "event": "BALLOON_CHANGE", "data": {"actual": 1073610752}} {"timestamp": {"seconds": 1693272174, "microseconds": 89704}, \ "event": "BALLOON_CHANGE", "data": {"actual": 1073610752}} {"timestamp": {"seconds": 1693272175, "microseconds": 90819}, \ "event": "BALLOON_CHANGE", "data": {"actual": 1073610752}} {"timestamp": {"seconds": 1693272176, "microseconds": 91961}, \ "event": "BALLOON_CHANGE", "data": {"actual": 1073610752}} {"timestamp": {"seconds": 1693272177, "microseconds": 93040}, \ "event": "BALLOON_CHANGE", "data": {"actual": 1073676288}} {"timestamp": {"seconds": 1693272178, "microseconds": 94117}, \ "event": "BALLOON_CHANGE", "data": {"actual": 1073676288}} {"timestamp": {"seconds": 1693272179, "microseconds": 95337}, \ "event": "BALLOON_CHANGE", "data": {"actual": 1073610752}} {"timestamp": {"seconds": 1693272180, "microseconds": 96615}, \ "event": "BALLOON_CHANGE", "data": {"actual": 1073676288}} {"timestamp": {"seconds": 1693272181, "microseconds": 97626}, \ "event": "BALLOON_CHANGE", "data": {"actual": 1073610752}} {"timestamp": {"seconds": 1693272182, "microseconds": 98693}, \ "event": "BALLOON_CHANGE", "data": {"actual": 1073676288}} {"timestamp": {"seconds": 1693272183, "microseconds": 99698}, \ "event": "BALLOON_CHANGE", "data": {"actual": 1073610752}} {"timestamp": {"seconds": 1693272184, "microseconds": 100727}, \ "event": "BALLOON_CHANGE", "data": {"actual": 1073610752}} {"timestamp": {"seconds": 1693272185, "microseconds": 90430}, \ "event": "BALLOON_CHANGE", "data": {"actual": 1073610752}} {"timestamp": {"seconds": 1693272186, "microseconds": 102999}, \ "event": "BALLOON_CHANGE", "data": {"actual": 1073676288}} : Fix it by having the target aligned to the guest page size, 64KB in this specific case. With this applied, no flooding QMP event is observed and the memory balloon's size can be stablizied to 0x3ffe0000 soon after the deflation request is sent. { "execute" : "balloon", "arguments": { "value" : 1073672192 } } {"return": {}} {"timestamp": {"seconds": 1693273328, "microseconds": 793075}, \ "event": "BALLOON_CHANGE", "data": {"actual": 1073610752}} { "execute" : "query-balloon" } {"return": {"actual": 1073610752}} Signed-off-by: Gavin Shan Tested-by: Zhenyu Zhang --- drivers/virtio/virtio_balloon.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloo= n.c index 5b15936a5214..625caac35264 100644 --- a/drivers/virtio/virtio_balloon.c +++ b/drivers/virtio/virtio_balloon.c @@ -386,6 +386,17 @@ static void stats_handle_request(struct virtio_balloon= *vb) virtqueue_kick(vq); } =20 +static inline s64 align_pages_up(s64 diff) +{ + if (diff =3D=3D 0) + return diff; + + if (diff > 0) + return ALIGN(diff, VIRTIO_BALLOON_PAGES_PER_PAGE); + + return -ALIGN(-diff, VIRTIO_BALLOON_PAGES_PER_PAGE); +} + static inline s64 towards_target(struct virtio_balloon *vb) { s64 target; @@ -396,7 +407,7 @@ static inline s64 towards_target(struct virtio_balloon = *vb) &num_pages); =20 target =3D num_pages; - return target - vb->num_pages; + return align_pages_up(target - vb->num_pages); } =20 /* Gives back @num_to_return blocks of free pages to mm. */ --=20 2.41.0