Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp5106950ybc; Fri, 15 Nov 2019 14:58:52 -0800 (PST) X-Google-Smtp-Source: APXvYqyLNruTUqg3RxQjGzxVYJE3dgoynr61OW242h+8vteEAV9ye+AzQ0qNcHn0l1UG0fLn51og X-Received: by 2002:a17:906:5959:: with SMTP id g25mr4526394ejr.248.1573858732429; Fri, 15 Nov 2019 14:58:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1573858732; cv=none; d=google.com; s=arc-20160816; b=prbKBdnmlZTHOXc4U8vzcvzIBOgV3CqmGxs6Zt90Q4clRs5UnzEImx/rVCPKsoTDJ5 JS5x1wGcao1VMWrhxRe8sHRW0/FyhrmPwMHxgo4BAoLUqtHOdRRbjxXloaL0PeDA4/wt 3QlVS4KqVvPW5/Lu3hYB9WcL7d3wijoEcdjduiwJ0Kie5rrXJIywXiEW3gcEEWDYyHTB eRItnzZumWSNaWTsi1ghwU6gPyQtLzonTvzNjJFZPUl0Ds5vlanaF75A5a5gAgJQe/dF uFwT2Ym8bBUQHwuL1CJWHNegDUBtYSehDJUlterPfbuS35QG73zn1u15t9bwpdulZp8L yubg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:mime-version :message-id:date:dkim-signature; bh=053BG20b593X/SMWxmhUI3JuwfglYUEj/JJwvN0h+wU=; b=nIeBflaGLYKfd9FmyjqbpVGkRidHL/1z7WH/u3Fn0O7T/I2UEQZlXKdWfDLhHw1s9m xOLONPsU5jA7N5Dl222/I9VQ6VXV5fBAARyASTKf2bwnQxelVzD4KhYx2UyFcxDNWrbl fOilxnuXSpa1GuBvPga7M/3cfUeeMPrTJRofTDygCQzePO1HVFVDk85D1I+CBeiD3haD p8Cv159HyggfPbkmcp1dBy/dQvMHFRGIJz0ZSUoV95xTXerPnH6JtHUQ1WfYjepSsZgS r6um9/MY7/hdqV5nzCPGfcfWOJxZGCms+ViO2CF5xcFzU2lcsyU9kP4XqKOCCS4Wt8Qd YbMQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=OgszDbwy; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id gx4si7000144ejb.182.2019.11.15.14.58.27; Fri, 15 Nov 2019 14:58:52 -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=@google.com header.s=20161025 header.b=OgszDbwy; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727036AbfKOW5U (ORCPT + 99 others); Fri, 15 Nov 2019 17:57:20 -0500 Received: from mail-vs1-f74.google.com ([209.85.217.74]:38549 "EHLO mail-vs1-f74.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726973AbfKOW5T (ORCPT ); Fri, 15 Nov 2019 17:57:19 -0500 Received: by mail-vs1-f74.google.com with SMTP id b63so1724373vsb.5 for ; Fri, 15 Nov 2019 14:57:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=053BG20b593X/SMWxmhUI3JuwfglYUEj/JJwvN0h+wU=; b=OgszDbwyMvl11IRddPSG1mPf/uKCmY4IqAIBsD9+gVpRRzbJNw/mGNYvXxiVPrFVlW Aan5NICVu021n+/8xCWhTuE9mSOagqWWylR2U1uKY8N0c85LOHRC1nUM/upCmSciUrMm L3fXvrx1Uk2QZW3t0IA3VZMFUloVadHsmDDW+KPK4k4GT7VKWyNZdrh8ttIgPHliRJeX oQcAUGnn+otpz+RosyXPRGtGt4m+DI5iIbRw8uYGN1wbpdHiKugQcL14vgh+MrAYrh+Q O5rLxF4hJC6r+aDPdWf2qGbrnRjt4jg41pNONTu21HGtbs4rq2MKcszV50lWWRTdmALY fagw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=053BG20b593X/SMWxmhUI3JuwfglYUEj/JJwvN0h+wU=; b=rJpEC/OSMnCFf8f2hoI71lGouirp7OUQ4uO6yZQPBfuXCwItChX/svbD9b2UJ2r8w5 2D8HrtbTvujVYIfKzkjwfMaCg597BLirYpkEev5uQ2IwP5tYp2f9HyyD6xQmuWCb+E9M lNzlLLsPOYCOqIqfTyTU7V3vG/6zqum5rZEQGLTqgAszyEPvQkKkYxuE/v82nn6HwniS +hCGjLDxuLmckBe6XD+NP9Ol/USM6CadKJmqnT2f9o4ZmOXRGrBb0wYH8FswKmic7Z7v xAAluXh0kThGUHiEYJ8jl48N9HTWHAEuJakP2io70DClLz2jxqYBdi63226G6J9xUXxK 4T8g== X-Gm-Message-State: APjAAAV74+Bo++LAsmzzztFpKeMxIrar/MxGYJ3ElIPQcAVpfCFConil kzYI76iNlANHOflnd1WtaVkmpqr+us8= X-Received: by 2002:a67:c995:: with SMTP id y21mr2878122vsk.158.1573858638823; Fri, 15 Nov 2019 14:57:18 -0800 (PST) Date: Fri, 15 Nov 2019 14:55:57 -0800 Message-Id: <20191115225557.61847-1-khazhy@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.24.0.432.g9d3f5f5b63-goog Subject: [PATCH] virtio_balloon: fix shrinker pages_to_free calculation From: Khazhismel Kumykov To: mst@redhat.com, jasowang@redhat.com, wei.w.wang@intel.com Cc: virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Khazhismel Kumykov Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. 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() 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); if (pages_freed >= pages_to_free) - return pages_freed; + return pages_freed / VIRTIO_BALLOON_PAGES_PER_PAGE; pages_freed += shrink_balloon_pages(vb, pages_to_free - pages_freed); - return pages_freed; + return pages_freed / VIRTIO_BALLOON_PAGES_PER_PAGE; } static unsigned long virtio_balloon_shrinker_count(struct shrinker *shrinker, -- 2.24.0.432.g9d3f5f5b63-goog