From patchwork Sun Mar 22 16:04:57 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hui Zhu X-Patchwork-Id: 11451859 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 915DE1667 for ; Sun, 22 Mar 2020 16:05:38 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 597EC20724 for ; Sun, 22 Mar 2020 16:05:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 597EC20724 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 9A2F46B0006; Sun, 22 Mar 2020 12:05:37 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 97B266B0007; Sun, 22 Mar 2020 12:05:37 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8B68D6B0008; Sun, 22 Mar 2020 12:05:37 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 78B216B0006 for ; Sun, 22 Mar 2020 12:05:37 -0400 (EDT) Received: from smtpin19.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 1907C8248047 for ; Sun, 22 Mar 2020 16:05:37 +0000 (UTC) X-FDA: 76623473514.19.river66_139f15bf7135d X-Spam-Summary: 2,0,0,d2a01916ef590b13,d41d8cd98f00b204,teawaterz@linux.alibaba.com,,RULES_HIT:2:41:355:379:541:960:966:973:988:989:1260:1261:1345:1359:1437:1535:1730:1747:1777:1792:1801:2196:2198:2199:2200:2393:2559:2562:2693:2731:2901:3138:3139:3140:3141:3142:3308:3355:3865:3866:3867:3868:3870:3871:3872:4049:4119:4250:4321:4384:4385:4395:4605:5007:6119:6261:6737:7903:8603:8957:9149:9413:9901:10004:11026:11232:11233:11473:11658:11914:12043:12048:12114:12291:12296:12297:12438:12555:12895:12986:13161:13229:13255:13869:14096:14394:14687:21080:21451:21627:21666:21966:21987:21990:30003:30005:30054,0,RBL:115.124.30.44:@linux.alibaba.com:.lbl8.mailshell.net-64.201.201.201 62.20.2.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:26,LUA_SUMMARY:none X-HE-Tag: river66_139f15bf7135d X-Filterd-Recvd-Size: 8269 Received: from out30-44.freemail.mail.aliyun.com (out30-44.freemail.mail.aliyun.com [115.124.30.44]) by imf02.hostedemail.com (Postfix) with ESMTP for ; Sun, 22 Mar 2020 16:05:34 +0000 (UTC) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R201e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e04400;MF=teawaterz@linux.alibaba.com;NM=1;PH=DS;RN=14;SR=0;TI=SMTPD_---0TtH5FIv_1584893121; Received: from localhost(mailfrom:teawaterz@linux.alibaba.com fp:SMTPD_---0TtH5FIv_1584893121) by smtp.aliyun-inc.com(127.0.0.1); Mon, 23 Mar 2020 00:05:28 +0800 From: Hui Zhu To: mst@redhat.com, jasowang@redhat.com, akpm@linux-foundation.org, mojha@codeaurora.org, pagupta@redhat.com, aquini@redhat.com, namit@vmware.com, david@redhat.com, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, qemu-devel@nongnu.org Cc: Hui Zhu , Hui Zhu Subject: [PATCH for QEMU v2] virtio-balloon: Add option cont-pages to set VIRTIO_BALLOON_VQ_INFLATE_CONT Date: Mon, 23 Mar 2020 00:04:57 +0800 Message-Id: <1584893097-12317-2-git-send-email-teawater@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1584893097-12317-1-git-send-email-teawater@gmail.com> References: <1584893097-12317-1-git-send-email-teawater@gmail.com> X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 cont-pages to on will open flags VIRTIO_BALLOON_VQ_INFLATE_CONT and set continuous pages order to THP order. Then It will get continuous pages PFN from VQ icvq use use madvise MADV_DONTNEED release the THP page. This will handle the THP split issue. Signed-off-by: Hui Zhu --- hw/virtio/virtio-balloon.c | 32 +++++++++++++++++++++---- include/hw/virtio/virtio-balloon.h | 4 +++- include/standard-headers/linux/virtio_balloon.h | 4 ++++ 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/hw/virtio/virtio-balloon.c b/hw/virtio/virtio-balloon.c index a4729f7..88bdaca 100644 --- a/hw/virtio/virtio-balloon.c +++ b/hw/virtio/virtio-balloon.c @@ -34,6 +34,7 @@ #include "hw/virtio/virtio-access.h" #define BALLOON_PAGE_SIZE (1 << VIRTIO_BALLOON_PFN_SHIFT) +#define CONT_PAGES_ORDER 9 typedef struct PartiallyBalloonedPage { ram_addr_t base_gpa; @@ -65,7 +66,8 @@ static bool virtio_balloon_pbp_matches(PartiallyBalloonedPage *pbp, static void balloon_inflate_page(VirtIOBalloon *balloon, MemoryRegion *mr, hwaddr mr_offset, - PartiallyBalloonedPage *pbp) + PartiallyBalloonedPage *pbp, + bool is_cont_pages) { void *addr = memory_region_get_ram_ptr(mr) + mr_offset; ram_addr_t rb_offset, rb_aligned_offset, base_gpa; @@ -76,6 +78,13 @@ static void balloon_inflate_page(VirtIOBalloon *balloon, /* XXX is there a better way to get to the RAMBlock than via a * host address? */ rb = qemu_ram_block_from_host(addr, false, &rb_offset); + + if (is_cont_pages) { + ram_block_discard_range(rb, rb_offset, + BALLOON_PAGE_SIZE << CONT_PAGES_ORDER); + return; + } + rb_page_size = qemu_ram_pagesize(rb); if (rb_page_size == BALLOON_PAGE_SIZE) { @@ -361,9 +370,10 @@ static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq) trace_virtio_balloon_handle_output(memory_region_name(section.mr), pa); if (!qemu_balloon_is_inhibited()) { - if (vq == s->ivq) { + if (vq == s->ivq || vq == s->icvq) { balloon_inflate_page(s, section.mr, - section.offset_within_region, &pbp); + section.offset_within_region, &pbp, + vq == s->icvq); } else if (vq == s->dvq) { balloon_deflate_page(s, section.mr, section.offset_within_region); } else { @@ -618,9 +628,12 @@ static size_t virtio_balloon_config_size(VirtIOBalloon *s) if (s->qemu_4_0_config_size) { return sizeof(struct virtio_balloon_config); } - if (virtio_has_feature(features, VIRTIO_BALLOON_F_PAGE_POISON)) { + if (virtio_has_feature(s->host_features, VIRTIO_BALLOON_F_CONT_PAGES)) { return sizeof(struct virtio_balloon_config); } + if (virtio_has_feature(features, VIRTIO_BALLOON_F_PAGE_POISON)) { + return offsetof(struct virtio_balloon_config, pages_order); + } if (virtio_has_feature(features, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) { return offsetof(struct virtio_balloon_config, poison_val); } @@ -646,6 +659,10 @@ static void virtio_balloon_get_config(VirtIODevice *vdev, uint8_t *config_data) cpu_to_le32(VIRTIO_BALLOON_CMD_ID_DONE); } + if (virtio_has_feature(dev->host_features, VIRTIO_BALLOON_F_CONT_PAGES)) { + config.pages_order = cpu_to_le32(CONT_PAGES_ORDER); + } + trace_virtio_balloon_get_config(config.num_pages, config.actual); memcpy(config_data, &config, virtio_balloon_config_size(dev)); } @@ -816,6 +833,11 @@ static void virtio_balloon_device_realize(DeviceState *dev, Error **errp) virtio_error(vdev, "iothread is missing"); } } + + if (virtio_has_feature(s->host_features, VIRTIO_BALLOON_F_CONT_PAGES)) { + s->icvq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output); + } + reset_stats(s); } @@ -916,6 +938,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("cont-pages", VirtIOBalloon, host_features, + VIRTIO_BALLOON_F_CONT_PAGES, 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/hw/virtio/virtio-balloon.h b/include/hw/virtio/virtio-balloon.h index d1c968d..61d2419 100644 --- a/include/hw/virtio/virtio-balloon.h +++ b/include/hw/virtio/virtio-balloon.h @@ -42,7 +42,7 @@ enum virtio_balloon_free_page_report_status { typedef struct VirtIOBalloon { VirtIODevice parent_obj; - VirtQueue *ivq, *dvq, *svq, *free_page_vq; + VirtQueue *ivq, *dvq, *svq, *free_page_vq, *icvq; uint32_t free_page_report_status; uint32_t num_pages; uint32_t actual; @@ -70,6 +70,8 @@ typedef struct VirtIOBalloon { uint32_t host_features; bool qemu_4_0_config_size; + + uint32_t pages_order; } VirtIOBalloon; #endif diff --git a/include/standard-headers/linux/virtio_balloon.h b/include/standard-headers/linux/virtio_balloon.h index 9375ca2..ee18be7 100644 --- a/include/standard-headers/linux/virtio_balloon.h +++ b/include/standard-headers/linux/virtio_balloon.h @@ -36,6 +36,8 @@ #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_CONT_PAGES 5 /* VQ to report continuous pages */ + /* Size of a PFN in the balloon interface. */ #define VIRTIO_BALLOON_PFN_SHIFT 12 @@ -51,6 +53,8 @@ struct virtio_balloon_config { uint32_t free_page_report_cmd_id; /* Stores PAGE_POISON if page poisoning is in use */ uint32_t poison_val; + /* Pages order if VIRTIO_BALLOON_F_CONT_PAGES is set */ + uint32_t pages_order; }; #define VIRTIO_BALLOON_S_SWAP_IN 0 /* Amount of memory swapped in */