linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] virtio_balloon: fix shrinker pages_to_free calculation
@ 2019-11-15 22:55 Khazhismel Kumykov
  2019-11-18  4:01 ` Wei Wang
  2019-11-18  5:26 ` Michael S. Tsirkin
  0 siblings, 2 replies; 16+ messages in thread
From: Khazhismel Kumykov @ 2019-11-15 22:55 UTC (permalink / raw)
  To: mst, jasowang, wei.w.wang
  Cc: virtualization, linux-kernel, Khazhismel Kumykov

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 <wei.w.wang@intel.com>
Signed-off-by: Khazhismel Kumykov <khazhy@google.com>
---

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


^ permalink raw reply related	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2019-11-19  9:37 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-11-15 22:55 [PATCH] virtio_balloon: fix shrinker pages_to_free calculation Khazhismel Kumykov
2019-11-18  4:01 ` Wei Wang
2019-11-18  5:30   ` Michael S. Tsirkin
2019-11-18  7:42     ` Wei Wang
2019-11-18  8:26       ` Michael S. Tsirkin
2019-11-18 10:31         ` Wei Wang
2019-11-18  5:26 ` Michael S. Tsirkin
2019-11-18 21:30   ` Khazhismel Kumykov
2019-11-18 21:38     ` [PATCH 1/2] virtio_balloon: fix " Khazhismel Kumykov
2019-11-18 21:38       ` [PATCH 2/2] virtio_balloon: fix shrinker_scan return units Khazhismel Kumykov
2019-11-18 22:56         ` Michael S. Tsirkin
2019-11-18 22:52       ` [PATCH 1/2] virtio_balloon: fix pages_to_free calculation Michael S. Tsirkin
2019-11-18 23:08       ` Michael S. Tsirkin
2019-11-18 23:20         ` Khazhismel Kumykov
2019-11-19  5:38         ` Wei Wang
2019-11-19  9:37           ` Michael S. Tsirkin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).