Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp2424841ybc; Sun, 17 Nov 2019 21:34:19 -0800 (PST) X-Google-Smtp-Source: APXvYqwntY3GegUbIlqU0P0Ayspag4EtinjmmrEW4xwIrFRUN5wsovFO8NvmmoDQFmFd2SdINTlI X-Received: by 2002:a17:906:8305:: with SMTP id j5mr12520650ejx.48.1574055259239; Sun, 17 Nov 2019 21:34:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574055259; cv=none; d=google.com; s=arc-20160816; b=MPEVwYMLOssAZ2jo3k8E6J/boBS36GjGjwGIkcYJpzPKnjypb+GkU9+3d7eF56guz5 Gmi2GXO70qk4ChH1MnsAgYzqnb/2bagE0glphVFjJnia3Af0ZJlWikfE5s7KvojYE6Qs L9jrMU7YqmhNOPfMUJDL2HH83GIxqj7VnhvXsa+kKQoX9D2925+PSRd8Ivj4ZXNZ2283 uRHQ+5cRPrtS1IJy7/4bQmh+/GqdWdHi5Crs7ST0tsrhJ+Bijz2avBGtoqkRmWpryr8n Y+kiO7TDzTCCjxc9dxduymANHdYBhO6SNOiq7wat0MDD+WWpBGCB9zOQiPGPtvWk/Fun A9tw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-disposition :content-transfer-encoding:in-reply-to:mime-version:references :message-id:subject:cc:to:from:date:dkim-signature; bh=cM+ZY0kiEN3ShFD9N2PS2oAJIZM6V3iyQ3TkZ3AlUbY=; b=FwZn6d/AM7bbynMj2iuZJuzylX1jCJiRQYbGdw6WezJ7BxJ0T8PbXPXR6bFH+7PqUw l1822XJ+9DCnrZ/yko2S8f2wGr+xu13avHUab1Q8WS76+KOvJxmD4NL8xpJ3O5JjPKH4 bANfqv5PeA5c+pZiuXyF+NT2KUYBAAcvJhWgKLzW/6b786ObQUad+8ABavIu7Y/g2ELl 8uprAHMQFLW2TYDk05yCvLRixDpNgrDFxQIC7OgES7kD+zwJEJ0S24t4ZVJvD0y/taXy f5QKTEVXNWdMVmB9v1n5ytYaFAs5px2mjaXOgzVQMGHv9uO0aPeqd+GSl2sWz/ZxpX9D Y9bw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=LBeV8noD; 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=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k24si12286343edk.367.2019.11.17.21.33.54; Sun, 17 Nov 2019 21:34:19 -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; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=LBeV8noD; 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=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726246AbfKRFaa (ORCPT + 99 others); Mon, 18 Nov 2019 00:30:30 -0500 Received: from us-smtp-2.mimecast.com ([207.211.31.81]:35960 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725208AbfKRFa3 (ORCPT ); Mon, 18 Nov 2019 00:30:29 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1574055028; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=cM+ZY0kiEN3ShFD9N2PS2oAJIZM6V3iyQ3TkZ3AlUbY=; b=LBeV8noDWxuhirUpcFrJPYrpoTGeNzhZEYCJk/T7zL5lrJqnsFZxXioMS1h8bgZfIGCZwH Khe88jt0YZRlIK0kHBlD/3LuYhAWTatAmxZyFSQyNE7TqmYJs97QtIH715+jx6oOiFmPKS C1xLXdVLCVqhAccQgfRiHBbuoaDzJbw= Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-171-L3kf4AFEN_yDsXBWCZq3mw-1; Mon, 18 Nov 2019 00:30:27 -0500 Received: by mail-qv1-f72.google.com with SMTP id w5so11784958qvp.13 for ; Sun, 17 Nov 2019 21:30:27 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=5WVKfymr1YEchUwz2Wftxp/wUTNabqDDc/q/WfqIOeU=; b=FQ7TqzamOikBV8geOFRZuoFPqUt7UTAy3kCNtSj7HGNpVrFxrxWUydlLiBjWBJgdFz N+jMnBNcX4g+k63xfbavNE9ynNMbCkdxHFhvcIuQvaG6b3hMYB4JeoZt87dxaAKANvcG f0DSlYxP7egW9YRh3sfvI+Ogn3eKS4ESsIbUOTsVqH05bjfFjFyJeO+BhOHKt97I3EXO D41m3/zNgmLmsAC2yzMpyQd8ppqNGPNlRerBkn+2Yim7ufmnkj6SyReSgrvAV5G2MTuy 1Oy2Jn6BKPpIAiQs1NPyn+4e6CIrMQeX6Dh3wKOzr/CA32tsq5fnB8whtCigLSE91OYm reSw== X-Gm-Message-State: APjAAAUMR3c165BULok7sP0XBPzZya5trXdkFVTuNJwBFRAbbumXr5sH hyHdOs285ZKmyTHTZzv31Lg4YG7wVuVvipmRnl7HJweY4vLg+9Js5vs6hmJ8F8iGfafutoQcQVE 6HXqlOzL9ZZDNRG5wBX1m55HP X-Received: by 2002:a37:4ad3:: with SMTP id x202mr16489671qka.362.1574055027371; Sun, 17 Nov 2019 21:30:27 -0800 (PST) X-Received: by 2002:a37:4ad3:: with SMTP id x202mr16489643qka.362.1574055026885; Sun, 17 Nov 2019 21:30:26 -0800 (PST) Received: from redhat.com (bzq-79-176-6-42.red.bezeqint.net. [79.176.6.42]) by smtp.gmail.com with ESMTPSA id w5sm7935124qkf.43.2019.11.17.21.30.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 17 Nov 2019 21:30:26 -0800 (PST) Date: Mon, 18 Nov 2019 00:30:21 -0500 From: "Michael S. Tsirkin" To: Wei Wang Cc: Khazhismel Kumykov , jasowang@redhat.com, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] virtio_balloon: fix shrinker pages_to_free calculation Message-ID: <20191118002652-mutt-send-email-mst@kernel.org> References: <20191115225557.61847-1-khazhy@google.com> <5DD21784.8020506@intel.com> MIME-Version: 1.0 In-Reply-To: <5DD21784.8020506@intel.com> X-MC-Unique: L3kf4AFEN_yDsXBWCZq3mw-1 X-Mimecast-Spam-Score: 0 Content-Type: text/plain; charset=WINDOWS-1252 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Nov 18, 2019 at 12:01:08PM +0800, Wei Wang wrote: > 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. >=20 > Not sure about the above. But the following unit mismatch is a good captu= re, > thanks! >=20 > >=20 > > 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. > >=20 > > 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. > >=20 > > (For 4K PAGE_SIZE, this mismatch wouldn't be noticed since > > VIRTIO_BALLOON_PAGES_PER_PAGE would be 1) > >=20 > > Have both return virtio pages, and divide into system pages when > > returning from shrinker_scan() >=20 > Sounds good. >=20 > >=20 > > Fixes: 71994620bb25 ("virtio_balloon: replace oom notifier with shrinke= r") > > Cc: Wei Wang > > Signed-off-by: Khazhismel Kumykov > > --- > >=20 > > Tested this under memory pressure conditions and the shrinker seemed to > > shrink. > >=20 > > drivers/virtio/virtio_balloon.c | 11 ++++------- > > 1 file changed, 4 insertions(+), 7 deletions(-) > >=20 > > diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_ba= lloon.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 v= irtio_balloon *vb, > > =09 * VIRTIO_BALLOON_ARRAY_PFNS_MAX balloon pages, so we call it > > =09 * multiple times to deflate pages till reaching pages_to_free. > > =09 */ > > -=09while (vb->num_pages && pages_to_free) { > > -=09=09pages_freed +=3D leak_balloon(vb, pages_to_free) / > > -=09=09=09=09=09VIRTIO_BALLOON_PAGES_PER_PAGE; > > -=09=09pages_to_free -=3D pages_freed; > > -=09} > > +=09while (vb->num_pages && pages_to_free > pages_freed) > > +=09=09pages_freed +=3D leak_balloon(vb, pages_to_free - pages_freed); > > =09update_balloon_size(vb); > > =09return pages_freed; > > @@ -805,11 +802,11 @@ static unsigned long virtio_balloon_shrinker_scan= (struct shrinker *shrinker, > > =09=09pages_freed =3D shrink_free_pages(vb, pages_to_free); >=20 > We also need a fix here then: >=20 > pages_freed =3D shrink_free_pages(vb, sc->nr_to_scan) * > VIRTIO_BALLOON_PAGES_PER_PAGE; No let's do accounting in pages please. virtio page is a legacy thing we just did not fix it in time to get rid of it by now. >=20 > Btw, there is another mistake, in virtio_balloon_shrinker_count: >=20 > - count +=3D vb->num_free_page_blocks >> VIRTIO_BALLOON_FREE_PAGE_O= RDER; > + count +=3D vb->num_free_page_blocks << VIRTIO_BALLOON_FREE_PAGE_OR= DER; >=20 > You may want to include it in this fix patch as well. OMG. should be a separate patch. But really this just shows why shifts are such a bad idea. Let's define VIRTIO_BALLOON_PAGES_PER_FREE_PAGE and use it with * and / consistently instead of shifts. > Best, > Wei >=20