Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp237135ybh; Thu, 12 Mar 2020 00:51:53 -0700 (PDT) X-Google-Smtp-Source: ADFU+vtvxDAD4yu/An8/lEOe0r4Cy5Q9+w8CtldxURUP7TrSdhVWqTTsqCNfpilMVzeqIN+AQ580 X-Received: by 2002:aca:dc56:: with SMTP id t83mr1758364oig.105.1583999513567; Thu, 12 Mar 2020 00:51:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1583999513; cv=none; d=google.com; s=arc-20160816; b=cAQsMHozgFazKSdSqgydL3vRRKCcQdNi8y4XtB9jDs7FaqLiw++czgVYEh16yZRXOt 5ulqNxn8wBGZjluOY7HUFf5glopIvj8ZxYokJ6kQzwVQOLn3CdjWKJEPCqSAyxL++uX3 AOXRLFRyoqwtOTHdW7kdo7P+8cL0b3FMzMxLxVsuuAz/TskYEpyDlplWBplGYQ7L+HwW QF52dOzz86Th+BfqMs24e/6K5nga3OZ5JRJvv7tQ+65ZWbWN+dYPQJfys0d/9iLSe9Hk 71OWxMzWyFaBr6RpIhEU3t4uHPQPLICcWD4U3IJFkUEqdZ5ogXYGIosfbXicbD2VWFA/ lgNw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=S9/asJ0NOKLSQHCQ3T0gUtjDgn/AlZuIf8Jps7mCVBk=; b=qU4NWgSSHeR0O37ndMWv3/lJzjg7M/jHS2zLp+QF0XYyx1oXDr+f7sT4ddaJ4wSvqf MelAPK+f0+OE9lgeRB0amdWECgNdeqsyxox0JPwqFRGAC9Jm03VP3whyJBUHLQpVH9kl A9eMzDuqlDVVSc5bqVC3GREKer5cqVJmDOjCdiZCpPupAh3Pg0tvl7y89ftGhWNPS1dN Ex37T3qgZXKShX8y4kE6nOdDLVSc2nD1Q6bvLtCPo3jcJ/3HaSEoDNsBnxyCPu0UZg1R kPJxetvy0NMQ/dJVfqVwlX1O/gdl/gOTGgYzGrhGxpYEpgM+u3hN4yosWeeReTzl1yut m4uQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d62si2188617oib.137.2020.03.12.00.51.41; Thu, 12 Mar 2020 00:51:53 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388128AbgCLHul (ORCPT + 99 others); Thu, 12 Mar 2020 03:50:41 -0400 Received: from out4436.biz.mail.alibaba.com ([47.88.44.36]:36150 "EHLO out4436.biz.mail.alibaba.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387973AbgCLHuk (ORCPT ); Thu, 12 Mar 2020 03:50:40 -0400 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R141e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01f04396;MF=teawaterz@linux.alibaba.com;NM=1;PH=DS;RN=12;SR=0;TI=SMTPD_---0TsNBE7I_1583999424; Received: from localhost(mailfrom:teawaterz@linux.alibaba.com fp:SMTPD_---0TsNBE7I_1583999424) by smtp.aliyun-inc.com(127.0.0.1); Thu, 12 Mar 2020 15:50:28 +0800 From: Hui Zhu To: mst@redhat.com, jasowang@redhat.com, akpm@linux-foundation.org, pagupta@redhat.com, mojha@codeaurora.org, david@redhat.com, namit@vmware.com, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, qemu-devel@nongnu.org Cc: Hui Zhu , Hui Zhu Subject: [RFC for QEMU] virtio-balloon: Add option thp-order to set VIRTIO_BALLOON_F_THP_ORDER Date: Thu, 12 Mar 2020 15:49:55 +0800 Message-Id: <1583999395-9131-2-git-send-email-teawater@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1583999395-9131-1-git-send-email-teawater@gmail.com> References: <1583999395-9131-1-git-send-email-teawater@gmail.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If the guest kernel has many fragmentation pages, use virtio_balloon will split THP of QEMU when it calls MADV_DONTNEED madvise to release the balloon pages. Set option thp-order to on will open flags VIRTIO_BALLOON_F_THP_ORDER. It will set balloon size to THP size to handle the THP split issue. Signed-off-by: Hui Zhu --- hw/virtio/virtio-balloon.c | 67 ++++++++++++++++--------- include/standard-headers/linux/virtio_balloon.h | 4 ++ 2 files changed, 47 insertions(+), 24 deletions(-) diff --git a/hw/virtio/virtio-balloon.c b/hw/virtio/virtio-balloon.c index a4729f7..cfe86b0 100644 --- a/hw/virtio/virtio-balloon.c +++ b/hw/virtio/virtio-balloon.c @@ -340,37 +340,49 @@ static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq) while (iov_to_buf(elem->out_sg, elem->out_num, offset, &pfn, 4) == 4) { unsigned int p = virtio_ldl_p(vdev, &pfn); hwaddr pa; + size_t handle_size = BALLOON_PAGE_SIZE; pa = (hwaddr) p << VIRTIO_BALLOON_PFN_SHIFT; offset += 4; - section = memory_region_find(get_system_memory(), pa, - BALLOON_PAGE_SIZE); - if (!section.mr) { - trace_virtio_balloon_bad_addr(pa); - continue; - } - if (!memory_region_is_ram(section.mr) || - memory_region_is_rom(section.mr) || - memory_region_is_romd(section.mr)) { - trace_virtio_balloon_bad_addr(pa); - memory_region_unref(section.mr); - continue; - } + if (virtio_has_feature(s->host_features, + VIRTIO_BALLOON_F_THP_ORDER)) + handle_size = BALLOON_PAGE_SIZE << VIRTIO_BALLOON_THP_ORDER; + + while (handle_size > 0) { + section = memory_region_find(get_system_memory(), pa, + BALLOON_PAGE_SIZE); + if (!section.mr) { + trace_virtio_balloon_bad_addr(pa); + continue; + } + if (!memory_region_is_ram(section.mr) || + memory_region_is_rom(section.mr) || + memory_region_is_romd(section.mr)) { + trace_virtio_balloon_bad_addr(pa); + memory_region_unref(section.mr); + continue; + } - trace_virtio_balloon_handle_output(memory_region_name(section.mr), - pa); - if (!qemu_balloon_is_inhibited()) { - if (vq == s->ivq) { - balloon_inflate_page(s, section.mr, - section.offset_within_region, &pbp); - } else if (vq == s->dvq) { - balloon_deflate_page(s, section.mr, section.offset_within_region); - } else { - g_assert_not_reached(); + trace_virtio_balloon_handle_output(memory_region_name(section.mr), + pa); + if (!qemu_balloon_is_inhibited()) { + if (vq == s->ivq) { + balloon_inflate_page(s, section.mr, + section.offset_within_region, + &pbp); + } else if (vq == s->dvq) { + balloon_deflate_page(s, section.mr, + section.offset_within_region); + } else { + g_assert_not_reached(); + } } + memory_region_unref(section.mr); + + pa += BALLOON_PAGE_SIZE; + handle_size -= BALLOON_PAGE_SIZE; } - memory_region_unref(section.mr); } virtqueue_push(vq, elem, offset); @@ -693,6 +705,8 @@ static void virtio_balloon_set_config(VirtIODevice *vdev, memcpy(&config, config_data, virtio_balloon_config_size(dev)); dev->actual = le32_to_cpu(config.actual); + if (virtio_has_feature(vdev->host_features, VIRTIO_BALLOON_F_THP_ORDER)) + dev->actual <<= VIRTIO_BALLOON_THP_ORDER; if (dev->actual != oldactual) { qapi_event_send_balloon_change(vm_ram_size - ((ram_addr_t) dev->actual << VIRTIO_BALLOON_PFN_SHIFT)); @@ -728,6 +742,9 @@ static void virtio_balloon_to_target(void *opaque, ram_addr_t target) } if (target) { dev->num_pages = (vm_ram_size - target) >> VIRTIO_BALLOON_PFN_SHIFT; + if (virtio_has_feature(dev->host_features, + VIRTIO_BALLOON_F_THP_ORDER)) + dev->num_pages >>= VIRTIO_BALLOON_THP_ORDER; virtio_notify_config(vdev); } trace_virtio_balloon_to_target(target, dev->num_pages); @@ -916,6 +933,8 @@ static Property virtio_balloon_properties[] = { VIRTIO_BALLOON_F_DEFLATE_ON_OOM, false), DEFINE_PROP_BIT("free-page-hint", VirtIOBalloon, host_features, VIRTIO_BALLOON_F_FREE_PAGE_HINT, false), + DEFINE_PROP_BIT("thp-order", VirtIOBalloon, host_features, + VIRTIO_BALLOON_F_THP_ORDER, false), /* QEMU 4.0 accidentally changed the config size even when free-page-hint * is disabled, resulting in QEMU 3.1 migration incompatibility. This * property retains this quirk for QEMU 4.1 machine types. diff --git a/include/standard-headers/linux/virtio_balloon.h b/include/standard-headers/linux/virtio_balloon.h index 9375ca2..f54d613 100644 --- a/include/standard-headers/linux/virtio_balloon.h +++ b/include/standard-headers/linux/virtio_balloon.h @@ -36,10 +36,14 @@ #define VIRTIO_BALLOON_F_DEFLATE_ON_OOM 2 /* Deflate balloon on OOM */ #define VIRTIO_BALLOON_F_FREE_PAGE_HINT 3 /* VQ to report free pages */ #define VIRTIO_BALLOON_F_PAGE_POISON 4 /* Guest is using page poisoning */ +#define VIRTIO_BALLOON_F_THP_ORDER 5 /* Set balloon page order to thp order */ /* Size of a PFN in the balloon interface. */ #define VIRTIO_BALLOON_PFN_SHIFT 12 +/* The order of the balloon page */ +#define VIRTIO_BALLOON_THP_ORDER 9 + #define VIRTIO_BALLOON_CMD_ID_STOP 0 #define VIRTIO_BALLOON_CMD_ID_DONE 1 struct virtio_balloon_config { -- 2.7.4