From patchwork Wed Apr 8 19:49:01 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Duyck X-Patchwork-Id: 11480737 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 CB81D15AB for ; Wed, 8 Apr 2020 19:51:00 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 9EDB32075E for ; Wed, 8 Apr 2020 19:51:00 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="MgqpimCt" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 9EDB32075E Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Received: from localhost ([::1]:40058 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jMGix-00055F-Ry for patchwork-qemu-devel@patchwork.kernel.org; Wed, 08 Apr 2020 15:50:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37243) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jMGh8-00021J-TS for qemu-devel@nongnu.org; Wed, 08 Apr 2020 15:49:08 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jMGh6-00036O-JK for qemu-devel@nongnu.org; Wed, 08 Apr 2020 15:49:06 -0400 Received: from mail-qk1-x744.google.com ([2607:f8b0:4864:20::744]:46011) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1jMGh6-00035C-9r for qemu-devel@nongnu.org; Wed, 08 Apr 2020 15:49:04 -0400 Received: by mail-qk1-x744.google.com with SMTP id m67so1525288qke.12 for ; Wed, 08 Apr 2020 12:49:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:from:to:cc:date:message-id:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=gbRBokZUeADxG1Js0n3CcSXLvNNKI4dY2r2GfqE2RsE=; b=MgqpimCt0H1OWTla793dPDrS3KHHa360iRORSuhXbeVPJFS8obt1JfELdDdPlEnJbR gHBXkVZJ9nRoiqyO7HzVU0A3UsrhcnRCAywDHJWWhTFDPjeEseBzCyhvnnbO6cI+rpNA hYwyHR6i0hS3Ox/Zgc3PxPczF6rw4BPdpU8R9QRoIRC89Xjc+RPxcLOg+BzVIRIyojmQ UaofJvfRbYPbEbSVyO6R2VQcbFOqG+wX473EUq1SmrQ4VcPcMdQxA2OecxOGwc9G5daQ +YfqKB4qd9swYoOAavUXvisL/dQvAnRQQ9MNLDMlkwfEa3Eosq7iCoJoiSA82kRt/m4d O+Ag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:cc:date:message-id:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=gbRBokZUeADxG1Js0n3CcSXLvNNKI4dY2r2GfqE2RsE=; b=GuU5b9Sg8idZP/+35T233BB4pXpEf3E8gX19w1dHRJcqVtyuB2gFuwC3AgHIg2+pT3 Ax1QyY7j5i19O/msE8ugWoNimFF43PUmZa0J7oe24TrJpBf2MFxGlifgXMY1h+cSt4Ge ZaKhV8V9lRyeCur+/rNbjkZNzLVkwxbHH+f6lRiP4FeuQ7+j1lUrVB3g9EdBH25t1lSI yoLfNyyIiLRejLS3I5towKVpfySuT1G08StPoH6bTnEsM+63j9RPtxnXBFbqYfHi+sK2 ifEqXzRnmMGMXwrh/i8nHqTh/DeD8brGj0PXN0PBOc+EC/pwDPpH28PG/hSi/biYYn/6 6Ttg== X-Gm-Message-State: AGi0PuZeBO75AC+zP71238RMnqneJoNvSF5AGAo2Pi9EUAy/UIEpjEi3 GkuQFT9VET4yU2bfL2B9BBtZu4vkLP8= X-Google-Smtp-Source: APiQypIQjGcgYg4WGbE7iWiimwp+tk8wjLK2fiN3pCooM7NfGBh4VE0u1+jO3zi1Imto1CLMi9XUCw== X-Received: by 2002:a05:620a:2016:: with SMTP id c22mr8992091qka.334.1586375343649; Wed, 08 Apr 2020 12:49:03 -0700 (PDT) Received: from localhost.localdomain ([2001:470:b:9c3:9e5c:8eff:fe4f:f2d0]) by smtp.gmail.com with ESMTPSA id y24sm6362658qtn.87.2020.04.08.12.49.02 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 08 Apr 2020 12:49:03 -0700 (PDT) Subject: [PATCH v17 RESUBMIT QEMU 3/3] virtio-balloon: Provide a interface for free page reporting From: Alexander Duyck To: david@redhat.com, mst@redhat.com Date: Wed, 08 Apr 2020 12:49:01 -0700 Message-ID: <20200408194901.24143.34991.stgit@localhost.localdomain> In-Reply-To: <20200408194628.24143.51644.stgit@localhost.localdomain> References: <20200408194628.24143.51644.stgit@localhost.localdomain> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::744 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: virtio-dev@lists.oasis-open.org, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" From: Alexander Duyck Add support for what I am referring to as "free page reporting". Basically the idea is to function very similar to how the balloon works in that we basically end up madvising the page as not being used. However we don't really need to bother with any deflate type logic since the page will be faulted back into the guest when it is read or written to. This is meant to be a simplification of the existing balloon interface to use for providing hints to what memory needs to be freed. I am assuming this is safe to do as the deflate logic does not actually appear to do very much other than tracking what subpages have been released and which ones haven't. Signed-off-by: Alexander Duyck --- hw/virtio/virtio-balloon.c | 46 ++++++++++++++++++++++++++++++++++-- include/hw/virtio/virtio-balloon.h | 2 +- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/hw/virtio/virtio-balloon.c b/hw/virtio/virtio-balloon.c index 455d85b7082f..5faafd2f62ac 100644 --- a/hw/virtio/virtio-balloon.c +++ b/hw/virtio/virtio-balloon.c @@ -321,6 +321,40 @@ static void balloon_stats_set_poll_interval(Object *obj, Visitor *v, balloon_stats_change_timer(s, 0); } +static void virtio_balloon_handle_report(VirtIODevice *vdev, VirtQueue *vq) +{ + VirtIOBalloon *dev = VIRTIO_BALLOON(vdev); + VirtQueueElement *elem; + + while ((elem = virtqueue_pop(vq, sizeof(VirtQueueElement)))) { + unsigned int i; + + for (i = 0; i < elem->in_num; i++) { + void *addr = elem->in_sg[i].iov_base; + size_t size = elem->in_sg[i].iov_len; + ram_addr_t ram_offset; + size_t rb_page_size; + RAMBlock *rb; + + if (qemu_balloon_is_inhibited() || dev->poison_val) + continue; + + rb = qemu_ram_block_from_host(addr, false, &ram_offset); + rb_page_size = qemu_ram_pagesize(rb); + + /* For now we will simply ignore unaligned memory regions */ + if ((ram_offset | size) & (rb_page_size - 1)) + continue; + + ram_block_discard_range(rb, ram_offset, size); + } + + virtqueue_push(vq, elem, 0); + virtio_notify(vdev, vq); + g_free(elem); + } +} + static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq) { VirtIOBalloon *s = VIRTIO_BALLOON(vdev); @@ -628,7 +662,8 @@ static size_t virtio_balloon_config_size(VirtIOBalloon *s) return sizeof(struct virtio_balloon_config); } if (virtio_has_feature(features, VIRTIO_BALLOON_F_PAGE_POISON) || - virtio_has_feature(features, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) { + virtio_has_feature(features, VIRTIO_BALLOON_F_FREE_PAGE_HINT) || + virtio_has_feature(features, VIRTIO_BALLOON_F_REPORTING)) { return sizeof(struct virtio_balloon_config); } return offsetof(struct virtio_balloon_config, free_page_report_cmd_id); @@ -716,7 +751,8 @@ static uint64_t virtio_balloon_get_features(VirtIODevice *vdev, uint64_t f, VirtIOBalloon *dev = VIRTIO_BALLOON(vdev); f |= dev->host_features; virtio_add_feature(&f, VIRTIO_BALLOON_F_STATS_VQ); - if (virtio_has_feature(f, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) { + if (virtio_has_feature(f, VIRTIO_BALLOON_F_FREE_PAGE_HINT) || + virtio_has_feature(f, VIRTIO_BALLOON_F_REPORTING)) { virtio_add_feature(&f, VIRTIO_BALLOON_F_PAGE_POISON); } @@ -806,6 +842,10 @@ static void virtio_balloon_device_realize(DeviceState *dev, Error **errp) s->dvq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output); s->svq = virtio_add_queue(vdev, 128, virtio_balloon_receive_stats); + if (virtio_has_feature(s->host_features, VIRTIO_BALLOON_F_REPORTING)) { + s->rvq = virtio_add_queue(vdev, 32, virtio_balloon_handle_report); + } + if (virtio_has_feature(s->host_features, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) { s->free_page_vq = virtio_add_queue(vdev, VIRTQUEUE_MAX_SIZE, @@ -939,6 +979,8 @@ static Property virtio_balloon_properties[] = { */ DEFINE_PROP_BOOL("qemu-4-0-config-size", VirtIOBalloon, qemu_4_0_config_size, false), + DEFINE_PROP_BIT("unused-page-reporting", VirtIOBalloon, host_features, + VIRTIO_BALLOON_F_REPORTING, true), DEFINE_PROP_LINK("iothread", VirtIOBalloon, iothread, TYPE_IOTHREAD, IOThread *), DEFINE_PROP_END_OF_LIST(), diff --git a/include/hw/virtio/virtio-balloon.h b/include/hw/virtio/virtio-balloon.h index 7fe78e5c14d7..db5bf7127112 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, *rvq; uint32_t free_page_report_status; uint32_t num_pages; uint32_t actual;