Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp2359667ybc; Sun, 17 Nov 2019 19:56:08 -0800 (PST) X-Google-Smtp-Source: APXvYqz1BCwhFtqcevzfFOw0JdIY9ap5C1tuL2NVXye2QZkzCLdJT7YPTgW7bpE4QxcD/2snOEXd X-Received: by 2002:a17:906:2e52:: with SMTP id r18mr23919047eji.178.1574049368274; Sun, 17 Nov 2019 19:56:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574049368; cv=none; d=google.com; s=arc-20160816; b=da4A6I0Hkuka/NJEZDzf/f4SwwWgXz0c89Yp8/IujUDLgkrZFTQehOSgbvgr2vg99N hTDODcBxIVlyzhqcxT/D1/EZyZ4Ib8jFvNV74aML6KqnEsMSm85+GHNLKSkfUDVy8UhW hD5o5QYt86GVdc40IPVLkV+9f6+2PEyAb7dxq6cywsQPOLYtIxBJ/LsAbe9YmUo3VxRW zHrDdwpTi01dTDuVjelx4yz/duJTVlVW0TJBIs0WZLclGkzpZOGsK88M8lSM5Ced6XyY uYLqWXsrLfUp4CFJNuW2Ue/2lwloqrEfJVlGnF0VbG8F0R9o5iq35Mc++wAJtKozowkX kkTw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:in-reply-to :references:subject:cc:to:mime-version:user-agent:from:date :message-id; bh=EnR8XlbMPpN8eeXBrQH/4LgwQSFfPRj7GUoSsNYG4iw=; b=uUXrH1UVGLQHp5kS8U4hwwrKAY9bsd+fUWKhaxQt6mTxNnjWypYSF0QWvJLmtozIJL agwWsRrurwlodn6TSqeil3YpNcoKQtkswafgcsUCvWBG8VAC6scuPuzH4yR+2lNOauf1 AkYjIDTFe6sWVK75dXAHvaKDGzwcLI5OMfyKrVrSd4r1T83BqGdrkcVM9bzzZ9d4b1NZ 0IeKO1eJkeajcDBmKfI8wjq2vjzzYMsCoNXIiJ/8OjcWCBn4MBmbCC29BLz3YxyUUIc6 2Da4kXo/RCV/cE7nJM8FIA1Tf9OoOcgrBOddZLEGfkcZijeg0WS3PTTmrNqljOiBDOzI xJhw== 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=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o25si10602596eja.316.2019.11.17.19.55.44; Sun, 17 Nov 2019 19:56:08 -0800 (PST) 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=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726371AbfKRDyZ (ORCPT + 99 others); Sun, 17 Nov 2019 22:54:25 -0500 Received: from mga09.intel.com ([134.134.136.24]:27563 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726314AbfKRDyZ (ORCPT ); Sun, 17 Nov 2019 22:54:25 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga102.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 17 Nov 2019 19:54:24 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.68,318,1569308400"; d="scan'208";a="258292816" Received: from unknown (HELO [10.239.13.7]) ([10.239.13.7]) by FMSMGA003.fm.intel.com with ESMTP; 17 Nov 2019 19:54:21 -0800 Message-ID: <5DD21784.8020506@intel.com> Date: Mon, 18 Nov 2019 12:01:08 +0800 From: Wei Wang User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 To: Khazhismel Kumykov , mst@redhat.com, jasowang@redhat.com CC: virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] virtio_balloon: fix shrinker pages_to_free calculation References: <20191115225557.61847-1-khazhy@google.com> In-Reply-To: <20191115225557.61847-1-khazhy@google.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 11/16/2019 06:55 AM, Khazhismel Kumykov wrote: > To my reading, we're accumulating total freed pages in pages_freed, but > subtracting it every iteration from pages_to_free, meaning we'll count > earlier iterations multiple times, freeing fewer pages than expected. > Just accumulate in pages_freed, and compare to pages_to_free. Not sure about the above. But the following unit mismatch is a good capture, thanks! > > There's also a unit mismatch, where pages_to_free seems to be virtio > balloon pages, and pages_freed is system pages (We divide by > VIRTIO_BALLOON_PAGES_PER_PAGE), so sutracting pages_freed from > pages_to_free may result in freeing too much. > > There also seems to be a mismatch between shrink_free_pages() and > shrink_balloon_pages(), where in both pages_to_free is given as # of > virtio pages to free, but free_pages() returns virtio pages, and > balloon_pages returns system pages. > > (For 4K PAGE_SIZE, this mismatch wouldn't be noticed since > VIRTIO_BALLOON_PAGES_PER_PAGE would be 1) > > Have both return virtio pages, and divide into system pages when > returning from shrinker_scan() Sounds good. > > Fixes: 71994620bb25 ("virtio_balloon: replace oom notifier with shrinker") > Cc: Wei Wang > Signed-off-by: Khazhismel Kumykov > --- > > Tested this under memory pressure conditions and the shrinker seemed to > shrink. > > drivers/virtio/virtio_balloon.c | 11 ++++------- > 1 file changed, 4 insertions(+), 7 deletions(-) > > diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c > index 226fbb995fb0..7951ece3fe24 100644 > --- a/drivers/virtio/virtio_balloon.c > +++ b/drivers/virtio/virtio_balloon.c > @@ -782,11 +782,8 @@ static unsigned long shrink_balloon_pages(struct virtio_balloon *vb, > * VIRTIO_BALLOON_ARRAY_PFNS_MAX balloon pages, so we call it > * multiple times to deflate pages till reaching pages_to_free. > */ > - while (vb->num_pages && pages_to_free) { > - pages_freed += leak_balloon(vb, pages_to_free) / > - VIRTIO_BALLOON_PAGES_PER_PAGE; > - pages_to_free -= pages_freed; > - } > + while (vb->num_pages && pages_to_free > pages_freed) > + pages_freed += leak_balloon(vb, pages_to_free - pages_freed); > update_balloon_size(vb); > > return pages_freed; > @@ -805,11 +802,11 @@ static unsigned long virtio_balloon_shrinker_scan(struct shrinker *shrinker, > pages_freed = shrink_free_pages(vb, pages_to_free); We also need a fix here then: pages_freed = shrink_free_pages(vb, sc->nr_to_scan) * VIRTIO_BALLOON_PAGES_PER_PAGE; Btw, there is another mistake, in virtio_balloon_shrinker_count: - count += vb->num_free_page_blocks >> VIRTIO_BALLOON_FREE_PAGE_ORDER; + count += vb->num_free_page_blocks << VIRTIO_BALLOON_FREE_PAGE_ORDER; You may want to include it in this fix patch as well. Best, Wei