virtio_balloon: fix shrinker pages_to_free calculation
Subsystems (2)
PATCH 1/1 virtio_balloon: fix shrinker pages_to_free calculation
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()
---
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(-)
drivers/virtio/virtio_balloon.c +4 -7
@@ -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,
Signature · 1 line
2.24.0.432.g9d3f5f5b63-goog
Quoted 4 lines · Khazhismel Kumykov
To my reading, we're accumulating total freed pages in
pages_freed, but subtracting it every iteration frompages_to_free, meaning we'll count earlier iterations multiple times, freeing fewer pages than expected. Just accumulate inpages_freed, and compare topages_to_free.
Not sure about the above. But the following unit mismatch is a good capture, thanks!
Quoted 11 lines
There's also a unit mismatch, where
pages_to_freeseems to be virtio balloon pages, andpages_freedis system pages (We divide byVIRTIO_BALLOON_PAGES_PER_PAGE), so sutractingpages_freedfrompages_to_freemay result in freeing too much.There also seems to be a mismatch between
shrink_free_pages()andshrink_balloon_pages(), where in bothpages_to_freeis given as # of virtio pages to free, butfree_pages()returns virtio pages, andballoon_pagesreturns system pages.
Quoted 5 lines
(For 4K
PAGE_SIZE, this mismatch wouldn't be noticed sinceVIRTIO_BALLOON_PAGES_PER_PAGEwould be 1)Have both return virtio pages, and divide into system pages when returning from
shrinker_scan()
Sounds good.
Quoted 31 lines
Fixes 71994620bb25 ("virtio_balloon: replace oom notifier with shrinker")Cc Wei Wang <[email protected]>Signed-off-by Khazhismel Kumykov <[email protected]>---
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(-)drivers/virtio/virtio_balloon.c +2
-5@@ -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;
drivers/virtio/virtio_balloon.c static unsigned long virtio_balloon_shrinker_scan(struct shrinker *shrinker, · line 802
Quoted 1 line
drivers/virtio/virtio_balloon.c +0
-0@@ -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
Michael S. Tsirkin
maintainerQuoted 62 lines · Wei Wang
Quoted 4 lines · Khazhismel Kumykov
To my reading, we're accumulating total freed pages in
pages_freed, but subtracting it every iteration frompages_to_free, meaning we'll count earlier iterations multiple times, freeing fewer pages than expected. Just accumulate inpages_freed, and compare topages_to_free.Not sure about the above. But the following unit mismatch is a good capture, thanks!
Quoted 11 lines
There's also a unit mismatch, where
pages_to_freeseems to be virtio balloon pages, andpages_freedis system pages (We divide byVIRTIO_BALLOON_PAGES_PER_PAGE), so sutractingpages_freedfrompages_to_freemay result in freeing too much.There also seems to be a mismatch between
shrink_free_pages()andshrink_balloon_pages(), where in bothpages_to_freeis given as # of virtio pages to free, butfree_pages()returns virtio pages, andballoon_pagesreturns system pages.Quoted 5 lines
(For 4K
PAGE_SIZE, this mismatch wouldn't be noticed sinceVIRTIO_BALLOON_PAGES_PER_PAGEwould be 1)Have both return virtio pages, and divide into system pages when returning from
shrinker_scan()Sounds good.
Quoted 30 lines
Fixes 71994620bb25 ("virtio_balloon: replace oom notifier with shrinker")Cc Wei Wang <[email protected]>Signed-off-by Khazhismel Kumykov <[email protected]>---
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(-)drivers/virtio/virtio_balloon.c +2
-5@@ -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;drivers/virtio/virtio_balloon.c static unsigned long virtio_balloon_shrinker_scan(struct shrinker *shrinker, · line 802
Quoted 1 line
drivers/virtio/virtio_balloon.c +0
-0@@ -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:
Quoted 2 lines
pages_freed=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.
Quoted 7 lines
Btw, there is another mistake, in
virtio_balloon_shrinker_count:(quoted diff) +1
-1- 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.
OMG. should be a separate patch.
But really this just shows why shifts are such a bad idea.
Let's defineVIRTIO_BALLOON_PAGES_PER_FREE_PAGE
and use it with * and / consistently instead of shifts.
Quoted 3 lines
Best,
Wei
Quoted 68 lines · Michael S. Tsirkin
Quoted 57 lines · Wei Wang
Quoted 4 lines · Khazhismel Kumykov
To my reading, we're accumulating total freed pages in
pages_freed, but subtracting it every iteration frompages_to_free, meaning we'll count earlier iterations multiple times, freeing fewer pages than expected. Just accumulate inpages_freed, and compare topages_to_free.Not sure about the above. But the following unit mismatch is a good capture, thanks!
Quoted 10 lines
There's also a unit mismatch, where
pages_to_freeseems to be virtio balloon pages, andpages_freedis system pages (We divide byVIRTIO_BALLOON_PAGES_PER_PAGE), so sutractingpages_freedfrompages_to_freemay result in freeing too much.There also seems to be a mismatch between
shrink_free_pages()andshrink_balloon_pages(), where in bothpages_to_freeis given as # of virtio pages to free, butfree_pages()returns virtio pages, andballoon_pagesreturns system pages.Quoted 5 lines
(For 4K
PAGE_SIZE, this mismatch wouldn't be noticed sinceVIRTIO_BALLOON_PAGES_PER_PAGEwould be 1)Have both return virtio pages, and divide into system pages when returning from
shrinker_scan()Sounds good.
Quoted 29 lines
Fixes 71994620bb25 ("virtio_balloon: replace oom notifier with shrinker")Cc Wei Wang <[email protected]>Signed-off-by Khazhismel Kumykov <[email protected]>---
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(-)drivers/virtio/virtio_balloon.c +2
-5@@ -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;drivers/virtio/virtio_balloon.c static unsigned long virtio_balloon_shrinker_scan(struct shrinker *shrinker, · line 802
Quoted 1 line
drivers/virtio/virtio_balloon.c +0
-0@@ -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:
Quoted 2 lines
pages_freed=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.
Quoted 5 lines
Btw, there is another mistake, in
virtio_balloon_shrinker_count:(quoted diff) +1
-1- count += vb->num_free_page_blocks >> VIRTIO_BALLOON_FREE_PAGE_ORDER; + count += vb->num_free_page_blocks << VIRTIO_BALLOON_FREE_PAGE_ORDER;
Quoted 9 lines
Quoted 1 line
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_PAGEand use it with * and / consistently instead of shifts.
OK, will do (maybe call it VIRTIO_BALLOON_FREE_PAGES_PER_BLOCK).
Best,
Wei
Michael S. Tsirkin
maintainerQuoted 76 lines · Wei Wang
Quoted 75 lines · Michael S. Tsirkin
Quoted 57 lines · Wei Wang
Quoted 4 lines · Khazhismel Kumykov
To my reading, we're accumulating total freed pages in
pages_freed, but subtracting it every iteration frompages_to_free, meaning we'll count earlier iterations multiple times, freeing fewer pages than expected. Just accumulate inpages_freed, and compare topages_to_free.Not sure about the above. But the following unit mismatch is a good capture, thanks!
Quoted 10 lines
There's also a unit mismatch, where
pages_to_freeseems to be virtio balloon pages, andpages_freedis system pages (We divide byVIRTIO_BALLOON_PAGES_PER_PAGE), so sutractingpages_freedfrompages_to_freemay result in freeing too much.There also seems to be a mismatch between
shrink_free_pages()andshrink_balloon_pages(), where in bothpages_to_freeis given as # of virtio pages to free, butfree_pages()returns virtio pages, andballoon_pagesreturns system pages.Quoted 5 lines
(For 4K
PAGE_SIZE, this mismatch wouldn't be noticed sinceVIRTIO_BALLOON_PAGES_PER_PAGEwould be 1)Have both return virtio pages, and divide into system pages when returning from
shrinker_scan()Sounds good.
Quoted 29 lines
Fixes 71994620bb25 ("virtio_balloon: replace oom notifier with shrinker")Cc Wei Wang <[email protected]>Signed-off-by Khazhismel Kumykov <[email protected]>---
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(-)drivers/virtio/virtio_balloon.c +2
-5@@ -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;drivers/virtio/virtio_balloon.c static unsigned long virtio_balloon_shrinker_scan(struct shrinker *shrinker, · line 802
Quoted 1 line
drivers/virtio/virtio_balloon.c +0
-0@@ -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:
Quoted 2 lines
pages_freed=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.
Quoted 6 lines
Btw, there is another mistake, in
virtio_balloon_shrinker_count:(quoted diff) +1
-1- 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.
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
Quoted 7 lines
Quoted 2 lines
and use it with * and / consistently instead of shifts.
OK, will do (maybe call it
VIRTIO_BALLOON_FREE_PAGES_PER_BLOCK).Best,
Wei
The order is called VIRTIO_BALLOON_FREE_PAGE_ORDER making it sounds like there's a free page and that's it order.
Let's rename that hont block?
So VIRTIO_BALLOON_HINT_BLOCK_ORDER ?
VIRTIO_BALLOON_PAGES_PER_HINT_BLOCK ?
Quoted 9 lines · Michael S. Tsirkin
The order is called
VIRTIO_BALLOON_FREE_PAGE_ORDERmaking it sounds like there's a free page and that's it order.Let's rename that hont block?
SoVIRTIO_BALLOON_HINT_BLOCK_ORDER?
VIRTIO_BALLOON_PAGES_PER_HINT_BLOCK?
Sounds good.
Best,
Wei
Michael S. Tsirkin
maintainerQuestion is, does all this cause any bugs?
I'm not against cleaning this up, not at all, but we need to know the impact.
Quoted 4 lines · Khazhismel Kumykov
To my reading, we're accumulating total freed pages in
pages_freed, but subtracting it every iteration frompages_to_free, meaning we'll count earlier iterations multiple times, freeing fewer pages than expected. Just accumulate inpages_freed, and compare topages_to_free.
For nr to scan: yes we scan less objects but that can only happen if the first pass does not free enough. And 1st pass can pass 256 entries, and my reading of code in do_shrink_slab is that it passes only as much as
#define SHRINK_BATCH 128
so it looks like this never triggers in practice - right?
Quoted 2 lines
There's also a unit mismatch,
So two unrelated issues, I think we want two patches.
Quoted 4 lines
where
pages_to_freeseems to be virtio
balloon pages, andpages_freedis system pages (We divide byVIRTIO_BALLOON_PAGES_PER_PAGE), so sutractingpages_freedfrompages_to_freemay result in freeing too much.
I am inclining to say we should pass in page units. Free page reporting is all done in units of MAX_ORDER - 1. Let's not ptopagate the crazy virtio page thing - we hopefully will add a saner interface to regular balloon too.
something like the below?
drivers/virtio/virtio_balloon.c +3 -3
@@ -783,8 +783,8 @@ static unsigned long shrink_balloon_pages(struct virtio_balloon *vb,* 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_freed +=leak_balloon (vb, pages_to_free * +VIRTIO_BALLOON_PAGES_PER_PAGE ); pages_to_free -= pages_freed; }update_balloon_size (vb);
@@ -799,7 +799,7 @@ static unsigned long virtio_balloon_shrinker_scan(struct shrinker *shrinker,struct virtio_balloon *vb =container_of (shrinker, struct virtio_balloon, shrinker); - pages_to_free = sc->nr_to_scan *VIRTIO_BALLOON_PAGES_PER_PAGE ; + pages_to_free = sc->nr_to_scan;if (virtio_has_feature (vb->vdev,VIRTIO_BALLOON_F_FREE_PAGE_HINT )) pages_freed =shrink_free_pages (vb, pages_to_free);
Quoted 7 lines
There also seems to be a mismatch between
shrink_free_pages()andshrink_balloon_pages(), where in bothpages_to_freeis given as # of virtio pages to free, butfree_pages()returns virtio pages, andballoon_pagesreturns system pages.(For 4K
PAGE_SIZE, this mismatch wouldn't be noticed sinceVIRTIO_BALLOON_PAGES_PER_PAGEwould be 1)
About return value:
The only
use for count_objects I see is:
freeable = shrinker->count_objects(shrinker, shrinkctl);
if (freeable == 0 || freeable == SHRINK_EMPTY)
return freeable;so units do not matter here.
For scan objects, IIUC they are eventually propagated to shrink_slab. That in turn is called at two sites. One ignores the returned value. The other does:
do {
struct mem_cgroup *memcg = NULL;
freed = 0;
memcg = mem_cgroup_iter(NULL, NULL, NULL);
do {
freed += shrink_slab(GFP_KERNEL, nid, memcg, 0);
} while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL);
} while (freed > 10);so returning a larger than real value because of double accounting will just make more calls to the scan function.
Quoted 10 lines
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 <[email protected]>Signed-off-by Khazhismel Kumykov <[email protected]>---
Tested this under memory pressure conditions and the shrinker seemed to shrink.
And to clarify, did you manage to detect it malfunctioning without the patch?
Quoted 37 lines
drivers/virtio/virtio_balloon.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-)drivers/virtio/virtio_balloon.c +4
-7@@ -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,Signature · 1 line
2.24.0.432.g9d3f5f5b63-goog
Quoted 12 lines · Michael S. Tsirkin
Question is, does all this cause any bugs?
I'm not against cleaning this up, not at all, but we need to know the impact.
Quoted 4 lines · Khazhismel Kumykov
To my reading, we're accumulating total freed pages in
pages_freed, but subtracting it every iteration frompages_to_free, meaning we'll count earlier iterations multiple times, freeing fewer pages than expected. Just accumulate inpages_freed, and compare topages_to_free.
Quoted 8 lines
For nr to scan: yes we scan less objects but that can only happen if the first pass does not free enough. And 1st pass can pass 256 entries, and my reading of code in
do_shrink_slabis that it passes only as much as
#defineSHRINK_BATCH128so it looks like this never triggers in practice - right?
As far as I could tell, there wasn't any significant real impact. It just raised an eyebrow as I was skimming over it.
SHRINK_BATCH is 128, it does look like we can override the batch size per shrinker if we desire, but we don't so it's 128, yeah.
Quoted 36 lines
Quoted 3 lines
There's also a unit mismatch,
So two unrelated issues, I think we want two patches.
Quoted 4 lines
where
pages_to_freeseems to be virtio
balloon pages, andpages_freedis system pages (We divide byVIRTIO_BALLOON_PAGES_PER_PAGE), so sutractingpages_freedfrompages_to_freemay result in freeing too much.I am inclining to say we should pass in page units. Free page reporting is all done in units of
MAX_ORDER- 1. Let's not ptopagate the crazy virtio page thing - we hopefully will add a saner interface to regular balloon too.something like the below?
drivers/virtio/virtio_balloon.c +2
-2@@ -783,8 +783,8 @@ static unsigned long shrink_balloon_pages(struct virtio_balloon *vb,* 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_freed +=leak_balloon (vb, pages_to_free * +VIRTIO_BALLOON_PAGES_PER_PAGE ); pages_to_free -= pages_freed; }update_balloon_size (vb);
drivers/virtio/virtio_balloon.c static unsigned long virtio_balloon_shrinker_scan(struct shrinker *shrinker, · line 805
Quoted 8 lines
drivers/virtio/virtio_balloon.c +1
-1@@ -799,7 +799,7 @@ static unsigned long virtio_balloon_shrinker_scan(struct shrinker *shrinker,struct virtio_balloon *vb =container_of (shrinker, struct virtio_balloon, shrinker); - pages_to_free = sc->nr_to_scan *VIRTIO_BALLOON_PAGES_PER_PAGE ; + pages_to_free = sc->nr_to_scan;if (virtio_has_feature (vb->vdev,VIRTIO_BALLOON_F_FREE_PAGE_HINT )) pages_freed =shrink_free_pages (vb, pages_to_free);
leak_balloon returns virtio pages so this would need to actually be something like pages_freed += leak_balloon(vb, pages_to_free * PAGES_PER_PAGE) / PAGES_PER_PAGE;, which didn't particularly sit well with me :)
since leak_balloon is used elsewhere and seems to use "virtio pages" I opted to have shrink_balloon accept number of "virtio pages", for consistency.
Quoted 49 lines
Quoted 8 lines
There also seems to be a mismatch between
shrink_free_pages()andshrink_balloon_pages(), where in bothpages_to_freeis given as # of virtio pages to free, butfree_pages()returns virtio pages, andballoon_pagesreturns system pages.(For 4K
PAGE_SIZE, this mismatch wouldn't be noticed sinceVIRTIO_BALLOON_PAGES_PER_PAGEwould be 1)About return value:
The only
use forcount_objectsI see is:freeable = shrinker->count_objects(shrinker, shrinkctl); if (freeable == 0 || freeable == SHRINK_EMPTY) return freeable;so units do not matter here.
For scan objects, IIUC they are eventually propagated to
shrink_slab. That in turn is called at two sites. One ignores the returned value. The other does:do { struct mem_cgroup *memcg = NULL; freed = 0; memcg = mem_cgroup_iter(NULL, NULL, NULL); do { freed += shrink_slab(GFP_KERNEL, nid, memcg, 0); } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL); } while (freed > 10);so returning a larger than real value because of double accounting will just make more calls to the scan function.
Quoted 8 lines
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 <[email protected]>Signed-off-by Khazhismel Kumykov <[email protected]>---
Quoted 6 lines
Quoted 2 lines
Tested this under memory pressure conditions and the shrinker seemed to shrink.
And to clarify, did you manage to detect it malfunctioning without the patch?
nope, just a cleanup.
I'll re-send my patches split up, but it sounds like there's some more incoming as well? I'll leave that to Wei.
khazhy
PATCH 1/2 virtio_balloon: fix pages_to_free calculation
freed_pages was accumulating total freed pages, but was also subtracted on each iteration from pages_to_free, which could potentially result in attempting to free fewer pages than asked for. This change also makes both freed_pages and pages_to_free in terms of "balloon pages", where they were mismatched before.
--- drivers/virtio/virtio_balloon.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/virtio/virtio_balloon.c +2 -5
@@ -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;
Signature · 1 line
2.24.0.432.g9d3f5f5b63-goog
PATCH 2/2 virtio_balloon: fix shrinker_scan return units
We were returning number of virtio balloon pages, which may not be the same as number of system pages
--- drivers/virtio/virtio_balloon.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/virtio/virtio_balloon.c +2 -2
@@ -802,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,
Signature · 1 line
2.24.0.432.g9d3f5f5b63-goog
Michael S. Tsirkin
maintainerRe: [PATCH 2/2] virtio_balloon: fix shrinker_scan return units
Quoted 15 lines · Khazhismel Kumykov
We were returning number of virtio balloon pages, which may not be the same as number of system pages
Fixes 86a559787e6f ("virtio-balloon: VIRTIO_BALLOON_F_FREE_PAGE_HINT")Cc Wei Wang <[email protected]>Signed-off-by Khazhismel Kumykov <[email protected]>--- drivers/virtio/virtio_balloon.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/virtio/virtio_balloon.c +0
-0
drivers/virtio/virtio_balloon.c static unsigned long virtio_balloon_shrinker_scan(struct shrinker *shrinker, · line 806
Quoted 6 lines
drivers/virtio/virtio_balloon.c +1
-1@@ -802,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 ;
I'm no seeing why is this one right. shrink_free_pages gives result in PAGE_SIZE units, right?
drivers/virtio/virtio_balloon.c static unsigned long virtio_balloon_shrinker_scan(struct shrinker *shrinker, · line 809
Quoted 4 lines
drivers/virtio/virtio_balloon.c +1
-1pages_freed +=shrink_balloon_pages (vb, pages_to_free - pages_freed); -return pages_freed; +return pages_freed /VIRTIO_BALLOON_PAGES_PER_PAGE ;
My head hurts. Isn't this what patch 1 tweaked?
Quoted 5 lines
drivers/virtio/virtio_balloon.c +0
-0} static unsigned long virtio_balloon_shrinker_count(struct shrinker *shrinker,Signature · 1 line
2.24.0.432.g9d3f5f5b63-goog
Michael S. Tsirkin
maintainerRe: [PATCH 1/2] virtio_balloon: fix pages_to_free calculation
In reply to Khazhismel KumykovQuoted 5 lines · Khazhismel Kumykov
freed_pageswas accumulating total freed pages, but was also subtracted on each iteration frompages_to_free, which could potentially result in attempting to free fewer pages than asked for. This change also makes bothfreed_pagesandpages_to_freein terms of "balloon pages", where they were mismatched before.
And then patch 2/2 changes it back to both be regular pages. Which is good, but why do we have to go back and forth breaking then fixing it back?
Quoted 28 lines
Fixes 71994620bb25 ("virtio_balloon: replace oom notifier with shrinker")Cc Wei Wang <[email protected]>Signed-off-by Khazhismel Kumykov <[email protected]>--- drivers/virtio/virtio_balloon.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)drivers/virtio/virtio_balloon.c +2
-5@@ -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;Signature · 1 line
2.24.0.432.g9d3f5f5b63-goog
Michael S. Tsirkin
maintainerRe: [PATCH 1/2] virtio_balloon: fix pages_to_free calculation
In reply to Khazhismel KumykovSo I really think we should do something like the below instead. Limit playing with balloon pages so we can gradually limit it to legacy. Testing, review would be appreciated.
drivers/virtio/virtio_balloon.c +11 -6
@@ -772,6 +772,13 @@ static unsigned long shrink_free_pages(struct virtio_balloon *vb,return blocks_freed <<VIRTIO_BALLOON_FREE_PAGE_ORDER ; } +static unsigned long leak_balloon_pages (struct virtio_balloon *vb, +unsigned long pages_to_free) +{ +return leak_balloon (vb, pages_to_free *VIRTIO_BALLOON_PAGES_PER_PAGE ) / +VIRTIO_BALLOON_PAGES_PER_PAGE ; +} +static unsigned long shrink_balloon_pages (struct virtio_balloon *vb,unsigned long pages_to_free) {
@@ -782,11 +789,9 @@ 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_freed < pages_to_free) + pages_freed +=leak_balloon_pages (vb, pages_to_free); +update_balloon_size (vb);return pages_freed;
@@ -799,7 +804,7 @@ static unsigned long virtio_balloon_shrinker_scan(struct shrinker *shrinker,struct virtio_balloon *vb =container_of (shrinker, struct virtio_balloon, shrinker); - pages_to_free = sc->nr_to_scan *VIRTIO_BALLOON_PAGES_PER_PAGE ; + pages_to_free = sc->nr_to_scan;if (virtio_has_feature (vb->vdev,VIRTIO_BALLOON_F_FREE_PAGE_HINT )) pages_freed =shrink_free_pages (vb, pages_to_free);
Re: [PATCH 1/2] virtio_balloon: fix pages_to_free calculation
Quoted 44 lines · Michael S. Tsirkin
So I really think we should do something like the below instead. Limit playing with balloon pages so we can gradually limit it to legacy. Testing, review would be appreciated.
Signed-off-by Michael S. Tsirkin <[email protected]>drivers/virtio/virtio_balloon.c +10
-5@@ -772,6 +772,13 @@ static unsigned long shrink_free_pages(struct virtio_balloon *vb,return blocks_freed <<VIRTIO_BALLOON_FREE_PAGE_ORDER ; } +static unsigned long leak_balloon_pages (struct virtio_balloon *vb, +unsigned long pages_to_free) +{ +return leak_balloon (vb, pages_to_free *VIRTIO_BALLOON_PAGES_PER_PAGE ) / +VIRTIO_BALLOON_PAGES_PER_PAGE ; +} +static unsigned long shrink_balloon_pages (struct virtio_balloon *vb,unsigned long pages_to_free) {@@ -782,11 +789,9 @@ 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_freed < pages_to_free) + pages_freed +=leak_balloon_pages (vb, pages_to_free); +update_balloon_size (vb);return pages_freed;
drivers/virtio/virtio_balloon.c static unsigned long virtio_balloon_shrinker_scan(struct shrinker *shrinker, · line 810
Quoted 8 lines
drivers/virtio/virtio_balloon.c +1
-1@@ -799,7 +804,7 @@ static unsigned long virtio_balloon_shrinker_scan(struct shrinker *shrinker,struct virtio_balloon *vb =container_of (shrinker, struct virtio_balloon, shrinker); - pages_to_free = sc->nr_to_scan *VIRTIO_BALLOON_PAGES_PER_PAGE ; + pages_to_free = sc->nr_to_scan;if (virtio_has_feature (vb->vdev,VIRTIO_BALLOON_F_FREE_PAGE_HINT )) pages_freed =shrink_free_pages (vb, pages_to_free);
I got confused about shrink_free_pages, since, looking at the operations the function *should* return the same units it takes in. However, it does look like it should be operating on system pages (VIRTIO_BALLOON_FREE_PAGE_ORDER is the order of our real page allocations), so the multiplication on nr_to_scan is where we messed up. OK. Sorry for the mixup :)
Your patch looks correct to me.
Wei Wang
Reviewed-byRe: [PATCH 1/2] virtio_balloon: fix pages_to_free calculation
In reply to Michael S. TsirkinQuoted 12 lines · Michael S. Tsirkin
So I really think we should do something like the below instead. Limit playing with balloon pages so we can gradually limit it to legacy. Testing, review would be appreciated.
Signed-off-by Michael S. Tsirkin <[email protected]>drivers/virtio/virtio_balloon.c +0
-0
drivers/virtio/virtio_balloon.c static unsigned long shrink_free_pages(struct virtio_balloon *vb, · line 781
Quoted 10 lines
drivers/virtio/virtio_balloon.c +7
-0@@ -772,6 +772,13 @@ static unsigned long shrink_free_pages(struct virtio_balloon *vb,return blocks_freed <<VIRTIO_BALLOON_FREE_PAGE_ORDER ; } +static unsigned long leak_balloon_pages (struct virtio_balloon *vb, +unsigned long pages_to_free) +{ +return leak_balloon (vb, pages_to_free *VIRTIO_BALLOON_PAGES_PER_PAGE ) / +VIRTIO_BALLOON_PAGES_PER_PAGE ; +} +
Looks good to me, too. (just a reminder that the returning type of leak_balloon is "unsigned int", we may want them to be consistent).
Best,
Wei
Michael S. Tsirkin
maintainerRe: [PATCH 1/2] virtio_balloon: fix pages_to_free calculation
Quoted 17 lines · Wei Wang
Quoted 16 lines · Michael S. Tsirkin
So I really think we should do something like the below instead. Limit playing with balloon pages so we can gradually limit it to legacy. Testing, review would be appreciated.
Signed-off-by Michael S. Tsirkin <[email protected]>drivers/virtio/virtio_balloon.c +2
-0@@ -772,6 +772,13 @@ static unsigned long shrink_free_pages(struct virtio_balloon *vb,return blocks_freed <<VIRTIO_BALLOON_FREE_PAGE_ORDER ; } +static unsigned long leak_balloon_pages (struct virtio_balloon *vb, +unsigned long pages_to_free)
Quoted 8 lines
drivers/virtio/virtio_balloon.c static unsigned long shrink_free_pages(struct virtio_balloon *vb, · line 781
Quoted 5 lines
drivers/virtio/virtio_balloon.c +5
-0+{ +return leak_balloon (vb, pages_to_free *VIRTIO_BALLOON_PAGES_PER_PAGE ) / +VIRTIO_BALLOON_PAGES_PER_PAGE ; +} +Looks good to me, too. (just a reminder that the returning type of
leak_balloonis "unsigned int",
Yea that use of 32 bit integers is another problem with the existing interfaces.
Quoted 6 lines
we may want them to be consistent).
Reviewed-by Wei Wang <[email protected]>Best,
Wei