Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp548035pxu; Tue, 5 Jan 2021 19:55:12 -0800 (PST) X-Google-Smtp-Source: ABdhPJzpkNLihbluvO23c4Yv2isLwLiRRKnf/2Of0xLQe1asPhN++zI/Apiv5iCi8IYgFwg9lgTZ X-Received: by 2002:aa7:c5d6:: with SMTP id h22mr2607643eds.82.1609905312149; Tue, 05 Jan 2021 19:55:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609905312; cv=none; d=google.com; s=arc-20160816; b=v2VSCL1z23vrRgJEX7/0o6+1FFKvjM6WLjwFKFQsJgd9KF20y4tRmGHEVuzHF5A/d2 lyfm0V6wjxAcxsNVzUQ11Hxtadv99725Tu6F5vMpQISSC5LGvGLsmFCb9aJ3tvyRnSrZ 4zSJUDp81FqayeLUn5+oIQ8fqYuWMgazuG+hT+a6XoMTzCaRgu6mh4efHFdZmCu2xvQw 9KZev/5clZurNsMeuFhF0YlA33Wd5aVwmU/1exX3gmsB/l01aysw8ec7Bd8UfrfrvRIw 9q5/Uoz2Nw84zJWzfBKoIUx8cDEzpc8y/mE25+Ma9jD+3kI8vr2B44+FKfpmBFnHKYV/ lp2w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:content-disposition:mime-version :mail-followup-to:message-id:subject:cc:to:date:from:dkim-signature; bh=eXrpMrj6YvsHKIRK9+LKKpoAFDK39SfbfHOXlypYzcY=; b=a2YU0I9wj4tYxK/xSvhmDTRBfEj91S7bHK79X8VVUF/jedFOGP94VHSq7YZ6XSe7fH EnahBXNhcgo/9FZWbejmSHPwRxmFrPtF8zt2n7Oi/pY7cJDRjjc2FiauMmYanShn9E3z ZebbUUXPMciQgpeM+MLpGilWRrQO+JQivG71j+WOHN69wMiVjak7XhOs78G6OXSfCW8Y EFQBcfXHHd3LLChek7W+4BY8QGWrlooN61SVjhyoB1iL3xLVZLSMUGnjGhuh6PfmpNAm 3zoM6sqS28Sq2npXnZrRFkgWh8fRK3+tO3+SSuYc/8vU9jbejTvqQl/03cAVKCS2GMwF kryg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=RvvSRBJL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id mj22si467877ejb.215.2021.01.05.19.54.48; Tue, 05 Jan 2021 19:55:12 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=RvvSRBJL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726499AbhAFDv6 (ORCPT + 99 others); Tue, 5 Jan 2021 22:51:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42314 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726074AbhAFDv5 (ORCPT ); Tue, 5 Jan 2021 22:51:57 -0500 Received: from mail-pf1-x432.google.com (mail-pf1-x432.google.com [IPv6:2607:f8b0:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 50BEAC06134C for ; Tue, 5 Jan 2021 19:51:17 -0800 (PST) Received: by mail-pf1-x432.google.com with SMTP id s21so935346pfu.13 for ; Tue, 05 Jan 2021 19:51:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:date:to:cc:subject:message-id:mail-followup-to:mime-version :content-disposition:user-agent; bh=eXrpMrj6YvsHKIRK9+LKKpoAFDK39SfbfHOXlypYzcY=; b=RvvSRBJL76JHBWv5Iuo5pbFPOkCRGhaqksMZdVAk3yc+voMyPuThBYLQuC1WqBWUet lDoluuYk8qyNnjbsrHBvLEm+r5ZN2NVlldQ0fPVErO5yiDlMCmPqjGCmqn9tdrpDLA1x q93rHGhvbSgH3HGS153bt7sjg5jfKMevbILEiujQKrjlFIw3GWYRTCRimjHBDGQwhNEP agCrtdxYbNowovcXKfSJ5JeVo3g91u/xu6Id0QxxYfihGyldf9ZujfBdQogUtcHYilXo FT9MrM86wlMFkA6n+jzSXjxlnEw74t2HBwgtaQzicWSZNenH4RXpPr8pU0RMB+7h1eQJ T5pA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:date:to:cc:subject:message-id :mail-followup-to:mime-version:content-disposition:user-agent; bh=eXrpMrj6YvsHKIRK9+LKKpoAFDK39SfbfHOXlypYzcY=; b=Hkian5HGaZH6tpEz6FdVQWVsYJifWjlgEhaSBqx59XPAJfZ0YND5Y0Ol37281MG+hc /WteMG8bpCWBBGReffzc1rdqMuXdLnODpKnreIKZtNZL+0uCdOT8EuABspLQ6GQXvbmt Btl/u+WZG4CJWFHqRsZ7Guz7hnu4Tqyh/ofLK1jFaYYa8KIFD0BnBokG2s0+hmeIJ6St HNTU+rJyy1yrYnffjn+qRlABS5Zlpwa7N0qu/QJYFWq/cgK571OafK/Qwvpiedw/b+qp qFEtRVjS+4DmsCL2En61kwr/D/kVCe22Qam4sILeV7bwkiC/ig97NEfzahsHVkwH2EKm RPRQ== X-Gm-Message-State: AOAM531sThBLwWm4QKSlmDkU9tNUyBrLmcgCDGpICFqv9jbp2UM+p2SU QZYDHieu1d7agkOmMkXbEWU= X-Received: by 2002:aa7:96c9:0:b029:1a3:c274:f717 with SMTP id h9-20020aa796c90000b02901a3c274f717mr2214501pfq.38.1609905076918; Tue, 05 Jan 2021 19:51:16 -0800 (PST) Received: from open-light-1.localdomain (66.98.113.28.16clouds.com. [66.98.113.28]) by smtp.gmail.com with ESMTPSA id g75sm743323pfb.2.2021.01.05.19.51.15 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 05 Jan 2021 19:51:16 -0800 (PST) From: Liang Li X-Google-Original-From: Liang Li Date: Tue, 5 Jan 2021 22:51:13 -0500 To: Alexander Duyck , Mel Gorman , Andrew Morton , Andrea Arcangeli , Dan Williams , "Michael S. Tsirkin" , David Hildenbrand , Jason Wang , Dave Hansen , Michal Hocko , Liang Li , Liang Li , Mike Kravetz Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, virtualization@lists.linux-foundation.org Subject: [PATCH 5/6] virtio-balloon: reporting hugetlb free page to host Message-ID: <20210106035110.GA1170@open-light-1.localdomain> Mail-Followup-To: Alexander Duyck , Mel Gorman , Andrew Morton , Andrea Arcangeli , Dan Williams , "Michael S. Tsirkin" , David Hildenbrand , Jason Wang , Dave Hansen , Michal Hocko , Liang Li , Mike Kravetz , linux-mm@kvack.org, linux-kernel@vger.kernel.org, virtualization@lists.linux-foundation.org MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Free page reporting only supports buddy pages, it can't report the free pages reserved for hugetlbfs case. On the other hand, hugetlbfs is a good choice for a system with a huge amount of RAM, because it can help to reduce the memory management overhead and improve system performance. This patch add support for reporting free hugepage to host when guest use hugetlbfs. Cc: Alexander Duyck Cc: Mel Gorman Cc: Andrea Arcangeli Cc: Dan Williams Cc: Dave Hansen Cc: David Hildenbrand Cc: Michal Hocko Cc: Andrew Morton Cc: Alex Williamson Cc: Michael S. Tsirkin Cc: Liang Li Signed-off-by: Liang Li --- drivers/virtio/virtio_balloon.c | 55 +++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c index 684bcc39ef5a..7bd7fcacee8c 100644 --- a/drivers/virtio/virtio_balloon.c +++ b/drivers/virtio/virtio_balloon.c @@ -126,6 +126,10 @@ struct virtio_balloon { /* Free page reporting device */ struct virtqueue *reporting_vq; struct page_reporting_dev_info pr_dev_info; + + /* Free hugepage reporting device */ + struct page_reporting_dev_info hpr_dev_info; + struct mutex mtx_report; }; static const struct virtio_device_id id_table[] = { @@ -173,6 +177,38 @@ static int virtballoon_free_page_report(struct page_reporting_dev_info *pr_dev_i struct virtqueue *vq = vb->reporting_vq; unsigned int unused, err; + mutex_lock(&vb->mtx_report); + /* We should always be able to add these buffers to an empty queue. */ + err = virtqueue_add_inbuf(vq, sg, nents, vb, GFP_NOWAIT | __GFP_NOWARN); + + /* + * In the extremely unlikely case that something has occurred and we + * are able to trigger an error we will simply display a warning + * and exit without actually processing the pages. + */ + if (WARN_ON_ONCE(err)) { + mutex_unlock(&vb->mtx_report); + return err; + } + + virtqueue_kick(vq); + + /* When host has read buffer, this completes via balloon_ack */ + wait_event(vb->acked, virtqueue_get_buf(vq, &unused)); + mutex_unlock(&vb->mtx_report); + + return 0; +} + +static int virtballoon_free_hugepage_report(struct page_reporting_dev_info *hpr_dev_info, + struct scatterlist *sg, unsigned int nents) +{ + struct virtio_balloon *vb = + container_of(hpr_dev_info, struct virtio_balloon, hpr_dev_info); + struct virtqueue *vq = vb->reporting_vq; + unsigned int unused, err; + + mutex_lock(&vb->mtx_report); /* We should always be able to add these buffers to an empty queue. */ err = virtqueue_add_inbuf(vq, sg, nents, vb, GFP_NOWAIT | __GFP_NOWARN); @@ -181,13 +217,16 @@ static int virtballoon_free_page_report(struct page_reporting_dev_info *pr_dev_i * are able to trigger an error we will simply display a warning * and exit without actually processing the pages. */ - if (WARN_ON_ONCE(err)) + if (WARN_ON_ONCE(err)) { + mutex_unlock(&vb->mtx_report); return err; + } virtqueue_kick(vq); /* When host has read buffer, this completes via balloon_ack */ wait_event(vb->acked, virtqueue_get_buf(vq, &unused)); + mutex_unlock(&vb->mtx_report); return 0; } @@ -984,9 +1023,11 @@ static int virtballoon_probe(struct virtio_device *vdev) } vb->pr_dev_info.report = virtballoon_free_page_report; + vb->hpr_dev_info.report = virtballoon_free_hugepage_report; if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) { unsigned int capacity; + mutex_init(&vb->mtx_report); capacity = virtqueue_get_vring_size(vb->reporting_vq); if (capacity < PAGE_REPORTING_CAPACITY) { err = -ENOSPC; @@ -999,6 +1040,14 @@ static int virtballoon_probe(struct virtio_device *vdev) err = page_reporting_register(&vb->pr_dev_info); if (err) goto out_unregister_oom; + + vb->hpr_dev_info.mini_order = MAX_ORDER - 1; + vb->hpr_dev_info.batch_size = 16 * 1024 * 1024; /* 16M */ + vb->hpr_dev_info.delay_jiffies = 2 * HZ; /* 2 seconds */ + err = hugepage_reporting_register(&vb->hpr_dev_info); + if (err) + goto out_unregister_oom; + } virtio_device_ready(vdev); @@ -1051,8 +1100,10 @@ static void virtballoon_remove(struct virtio_device *vdev) { struct virtio_balloon *vb = vdev->priv; - if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) + if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) { page_reporting_unregister(&vb->pr_dev_info); + hugepage_reporting_unregister(&vb->hpr_dev_info); + } if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM)) unregister_oom_notifier(&vb->oom_nb); if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) -- 2.18.2