virtio_balloon: high order allocation
Subsystems (3)
PATCH 1/1 virtio_balloon: high order allocation
At present, the VirtIO balloon device driver allocates pages one by one using alloc_page(), and frees them using put_page().
This changes it so that the driver allocates high order pages by using alloc_pages(), and frees them using __free_pages() if possible. By doing so, the CPU performance of inflation and deflation should be improved.
The effect of this change has been confirmed by benchmarks that measure the elapsed time of inflation and deflation.
The results are here:
16 pages inflation:
before: 119,779 ns
after : 115,655 ns (-3.4%)
64 pages inflation:
before: 156,977 ns
after : 150,961 ns (-3.8%)
256 pages inflation:
before: 218,649 ns
after : 208,490 ns (-4.6%)
16 pages deflation:
before: 78,112 ns
after : 68,288 ns (-12.6%)
64 pages deflation:
before: 97,205 ns
after : 80,491 ns (-17.194%)
256 pages deflation:
before: 122,742 ns
after : 107,526 ns (-12.4%)
--- drivers/virtio/virtio_balloon.c | 115 ++++++++++++----- include/linux/balloon_compaction.h | 54 ++++++-- mm/balloon_compaction.c | 193 ++++++++++++++++++++++------- 3 files changed, 273 insertions(+), 89 deletions(-)
drivers/virtio/virtio_balloon.c +83 -32
@@ -18,6 +18,7 @@#include <linux/wait.h> #include <linux/mm.h> #include <linux/page_reporting.h> +#include <linux/log2.h> /* * Balloon device works in 4K page units. So each page is pointed to by
@@ -25,7 +26,6 @@* page units. */ #define VIRTIO_BALLOON_PAGES_PER_PAGE (unsigned int)(PAGE_SIZE >> VIRTIO_BALLOON_PFN_SHIFT) -#define VIRTIO_BALLOON_ARRAY_PFNS_MAX 256/* Maximum number of (4k) pages to deflate on OOM notifications. */ #define VIRTIO_BALLOON_OOM_NR_PAGES 256#define VIRTIO_BALLOON_OOM_NOTIFY_PRIORITY 80
@@ -206,14 +206,23 @@ static unsigned int fill_balloon(struct virtio_balloon *vb, size_t num)unsigned int num_allocated_pages;unsigned int num_pfns;struct page *page; -LIST_HEAD (pages); +int order, i; +struct list_head page_lists[BALLOON_MAX_ORDER ]; + +for (order =0 ; order <BALLOON_MAX_ORDER ; order++) +INIT_LIST_HEAD (&page_lists[order]);/* We can only do one array worth at a time. */ num =min (num,ARRAY_SIZE (vb->pfns)); -for (num_pfns =0 ; num_pfns < num; - num_pfns +=VIRTIO_BALLOON_PAGES_PER_PAGE ) { -struct page *page =balloon_page_alloc (); + order =min (__ilog2_u64 (num),BALLOON_MAX_ORDER -1 ); +while (num >0 ) { +for (order =min (__ilog2_u64 (num), order); + order >=0 ; order--) { + page =balloon_pages_alloc (order); +if (page) +break ; + }if (!page) { dev_info_ratelimited(&vb->vdev->dev,
@@ -224,22 +233,36 @@ static unsigned int fill_balloon(struct virtio_balloon *vb, size_t num)break ; } -balloon_page_push (&pages, page); +balloon_page_push (&page_lists[order], page); + num_pfns = (1 << order); + num -= num_pfns; }mutex_lock (&vb->balloon_lock); vb->num_pfns =0 ; -while ((page =balloon_page_pop (&pages))) { -balloon_page_enqueue (&vb->vb_dev_info, page); - -set_page_pfns (vb, vb->pfns + vb->num_pfns, page); - vb->num_pages +=VIRTIO_BALLOON_PAGES_PER_PAGE ; -if (!virtio_has_feature (vb->vdev, -VIRTIO_BALLOON_F_DEFLATE_ON_OOM )) -adjust_managed_page_count (page,-1 ); - vb->num_pfns +=VIRTIO_BALLOON_PAGES_PER_PAGE ; +for (order =0 ; order <BALLOON_MAX_ORDER ; order++) { + num_pfns = (1 << order); +while ((page =balloon_page_pop (&page_lists[order]))) { +if ((balloon_page_enqueue ( + &vb->vb_dev_info, page, order))) { +WARN_ONCE (1 ,"invalid page or order: page=%p, order=%d" , + page, order); +break ; + } +for (i =0 ; i < num_pfns; i++) { +set_page_pfns (vb, vb->pfns + vb->num_pfns + i, +nth_page (page, i)); + } + vb->num_pages += + num_pfns *VIRTIO_BALLOON_PAGES_PER_PAGE ; +if (!virtio_has_feature ( + vb->vdev,VIRTIO_BALLOON_F_DEFLATE_ON_OOM )) +adjust_managed_page_count (page, -num_pfns); + vb->num_pfns += + num_pfns *VIRTIO_BALLOON_PAGES_PER_PAGE ; + } } num_allocated_pages = vb->num_pfns;
@@ -252,40 +275,68 @@ static unsigned int fill_balloon(struct virtio_balloon *vb, size_t num) }static void release_pages_balloon (struct virtio_balloon *vb, -struct list_head *pages) +struct list_head *page_lists) {struct page *page, *next; - -list_for_each_entry_safe (page, next, pages, lru) { -if (!virtio_has_feature (vb->vdev, -VIRTIO_BALLOON_F_DEFLATE_ON_OOM )) -adjust_managed_page_count (page,1 ); -list_del (&page->lru); -put_page (page);/* balloon reference */ +int order; + +for (order =0 ; order <BALLOON_MAX_ORDER ; order++) { +list_for_each_entry_safe (page, next, &page_lists[order], lru) { +if (!virtio_has_feature ( + vb->vdev,VIRTIO_BALLOON_F_DEFLATE_ON_OOM )) +adjust_managed_page_count (page,1 << order); +list_del (&page->lru); +__free_pages (page, order);/* balloon reference */ + } } }static unsigned int leak_balloon (struct virtio_balloon *vb,size_t num) {unsigned int num_freed_pages; +unsigned int num_pfns;struct page *page;struct balloon_dev_info *vb_dev_info = &vb->vb_dev_info; -LIST_HEAD (pages); +struct list_head page_lists[BALLOON_MAX_ORDER ]; +int order, i; + +for (order =0 ; order <BALLOON_MAX_ORDER ; order++) +INIT_LIST_HEAD (&page_lists[order]);/* We can only do one array worth at a time. */ num =min (num,ARRAY_SIZE (vb->pfns));mutex_lock (&vb->balloon_lock);/* We can't release more pages than taken */ - num =min (num, (size_t )vb->num_pages); -for (vb->num_pfns =0 ; vb->num_pfns < num; - vb->num_pfns +=VIRTIO_BALLOON_PAGES_PER_PAGE ) { - page =balloon_page_dequeue (vb_dev_info); + num =min_t (size_t, num, vb->num_pages); + + vb->num_pfns =0 ; + order =min (__ilog2_u64 (num),BALLOON_MAX_ORDER ); +while (num >0 ) { +for (order =min (__ilog2_u64 (num), order); + order >=0 ; order--) { + page =balloon_page_dequeue_by_order (vb_dev_info, order); + +if (page) +break ; + } + +if (IS_ERR (page)) { +WARN_ONCE (1 ,"invalid order (%d)\n" , order); +break ; + } +if (!page)break ; -set_page_pfns (vb, vb->pfns + vb->num_pfns, page); -list_add (&page->lru, &pages); - vb->num_pages -=VIRTIO_BALLOON_PAGES_PER_PAGE ; + + num_pfns = (1 << order); +for (i =0 ; i < num_pfns; i++) +set_page_pfns (vb, vb->pfns + vb->num_pfns + i,nth_page (page, i)); +list_add (&page->lru, &page_lists[order]); + vb->num_pages -= num_pfns *VIRTIO_BALLOON_PAGES_PER_PAGE ; + vb->num_pfns += num_pfns *VIRTIO_BALLOON_PAGES_PER_PAGE ; + + num -= num_pfns *VIRTIO_BALLOON_PAGES_PER_PAGE ; } num_freed_pages = vb->num_pfns;
@@ -296,7 +347,7 @@ static unsigned int leak_balloon(struct virtio_balloon *vb, size_t num)*/ if (vb->num_pfns !=0 )tell_host (vb, vb->deflate_vq); -release_pages_balloon (vb, &pages); +release_pages_balloon (vb, page_lists);mutex_unlock (&vb->balloon_lock);return num_freed_pages; }
include/linux/balloon_compaction.h +44 -10
101 lines not on this page · raw message
mm/balloon_compaction.c +146 -47
275 lines not on this page · raw message
Signature · 1 line
2.35.1
Quoted 23 lines · Soichiro Ueda
At present, the VirtIO balloon device driver allocates pages one by one using
alloc_page(), and frees them usingput_page().This changes it so that the driver allocates high order pages by using
alloc_pages(), and frees them using__free_pages()if possible. By doing so, the CPU performance of inflation and deflation should be improved.The effect of this change has been confirmed by benchmarks that measure the elapsed time of inflation and deflation.
The results are here:
16 pages inflation:
before: 119,779 ns after : 115,655 ns (-3.4%) 64 pages inflation: before: 156,977 ns after : 150,961 ns (-3.8%) 256 pages inflation: before: 218,649 ns after : 208,490 ns (-4.6%)16 pages deflation:
Quoted 8 lines
before: 78,112 ns after : 68,288 ns (-12.6%) 64 pages deflation: before: 97,205 ns after : 80,491 ns (-17.194%) 256 pages deflation: before: 122,742 ns after : 107,526 ns (-12.4%)
How does this affect page migration / balloon compaction etc?
Signature · 3 lines
Thanks, David / dhildenb
Hi David.
Quoted 1 line
How does this affect page migration / balloon compaction etc?
I guess this patch doesn't affect balloon compaction. When allocating pages using alloc_pages(), it skips compaction by masking out __GFP_RECLAIM if the order is larger than 0.
As for page migration, in the current implementation it migrate a 0-order page to another 0-order page. With this patch, it may migrate a high-order page to another same-order page.
But I noticed that the migrated high-order page is handled as 0-order page in virtballoon_migratepage().
Quoted 1 line
balloon_page_insert(vb_dev_info, newpage);
We should put the newpage into a page list of the corresponding order, like this.
balloon_page_enqueue(vb_dev_info, newpage, order);
I'll fix it in the v2 patch.
Thank you,
Quoted 8 lines · Soichiro Ueda
Hi David.
Quoted 1 line
How does this affect page migration / balloon compaction etc?
I guess this patch doesn't affect balloon compaction. When allocating pages using
alloc_pages(), it skips compaction by masking out__GFP_RECLAIMif the order is larger than 0.
Quoted 9 lines
As for page migration, in the current implementation it migrate a 0-order page to another 0-order page. With this patch, it may migrate a high-order page to another same-order page.
But I noticed that the migrated high-order page is handled as 0-order page in
virtballoon_migratepage().Quoted 1 line
balloon_page_insert(vb_dev_info, newpage);
Yes, I think suspected that it's broken.
We also might want to handle OOM accordingly by splitting the page and retrying migration. Almost nothing should stop a balloon page from getting migrated.
One thing to try is allocating a higher-order page and immediately splitting it into base pages, and enqueuing base pages only. Only inflation would be faster, because you could only deflate base pages.
Quoted 6 lines
We should put the newpage into a page list of the corresponding order, like this.
balloon_page_enqueue(vb_dev_info, newpage, order);I'll fix it in the v2 patch.
Note that I have some more concerns:
* We might end up stealing all higher-order pages from the guest instead of eating all of the "small" leftover pieces first. This might be undesirable. We discussed this in the past in the context of hugepage ballooning [not able to locate the abandoned patches quickly].
* PG_offline won't work as expected anymore and result in kdump reading inflated memory, which is undesirable. One workaround would be setting PG_offline on all base pages, but this needs some more thought.
* How are we handling a scenario where we are requested to deflate e.g., a single 4096 KiB page but we only have higher-order pages allocated? I suspect you would over-deflate right now, which is undesirable.
Signature · 3 lines
Thanks, David / dhildenb
Hi, Soichiro:
I'm interested with your patch. I'm looking at it and found some possible issues.
drivers/virtio/virtio_balloon.c static unsigned int fill_balloon(struct virtio_balloon *vb, size_t num) · line 257
Quoted 4 lines · Soichiro Ueda
drivers/virtio/virtio_balloon.c +4
-0+for (i =0 ; i < num_pfns; i++) { +set_page_pfns (vb, vb->pfns + vb->num_pfns + i, +nth_page (page, i)); + }
Since the interval of vb->num_pfns is VIRTIO_BALLOON_PAGES_PER_PAGE,
it seems that the pfns[] should be vb->pfns + vb->num_pfns + i * VIRTIO_BALLOON_PAGES_PER_PAGE.
drivers/virtio/virtio_balloon.c static unsigned int fill_balloon(struct virtio_balloon *vb, size_t num) · line 262
Quoted 5 lines
drivers/virtio/virtio_balloon.c +5
-0+ vb->num_pages += + num_pfns *VIRTIO_BALLOON_PAGES_PER_PAGE ; +if (!virtio_has_feature ( + vb->vdev,VIRTIO_BALLOON_F_DEFLATE_ON_OOM )) +adjust_managed_page_count (page, -num_pfns);
num_pfns is of the unsigned int type so need be to converted to long manually.
root@syzkaller:~# free -m
total used free shared buff/cache available
Mem: 1975 78 1822 2 74 1869
Swap: 0 0 0
root@syzkaller:~# QEMU 7.2.0 monitor - type 'help' for more information
(qemu) info balloon
balloon: actual=2048
(qemu) balloon 2046
(qemu)root@syzkaller:~# free -m
total used free shared buff/cache available
Mem: 33556405 33554510 1819 2 74 1867
Swap: 0 0 0drivers/virtio/virtio_balloon.c static unsigned int fill_balloon(struct virtio_balloon *vb, size_t num) · line 334
Quoted 4 lines
drivers/virtio/virtio_balloon.c +4
-0+ + num_pfns = (1 << order); +for (i =0 ; i < num_pfns; i++) +set_page_pfns (vb, vb->pfns + vb->num_pfns + i,nth_page (page, i));
The same problem mentioned above.
thanks,
Liu Shixin
.
Hi, Shixin.
Thanks for checking the patch!
Quoted 6 lines
drivers/virtio/virtio_balloon.c static unsigned int fill_balloon(struct virtio_balloon *vb, size_t num) · line 257
Quoted 4 lines
drivers/virtio/virtio_balloon.c +4
-0+for (i =0 ; i < num_pfns; i++) { +set_page_pfns (vb, vb->pfns + vb->num_pfns + i, +nth_page (page, i)); + }Since the interval of
vb->num_pfnsisVIRTIO_BALLOON_PAGES_PER_PAGE,
it seems that the pfns[] should bevb->pfns+vb->num_pfns+ i *VIRTIO_BALLOON_PAGES_PER_PAGE.
Definitely.
Quoted 6 lines
drivers/virtio/virtio_balloon.c static unsigned int fill_balloon(struct virtio_balloon *vb, size_t num) · line 262
Quoted 5 lines
drivers/virtio/virtio_balloon.c +5
-0+ vb->num_pages += + num_pfns *VIRTIO_BALLOON_PAGES_PER_PAGE ; +if (!virtio_has_feature ( + vb->vdev,VIRTIO_BALLOON_F_DEFLATE_ON_OOM )) +adjust_managed_page_count (page, -num_pfns);
num_pfnsis of the unsigned int type so need be to converted to long manually.
Oh, the second argument of adjust_managed_page_count is long type. So it needs casting as you say.
I'll fix these problems in the v2 patch.
Thank you,