virtio_balloon: batch large descriptor chains for page reporting
Subsystems (2)
PATCH 1/1 virtio_balloon: batch large descriptor chains for page reporting
The virtio_balloon driver reports a scatter-gather list of free pages to the VMM so it may discard unused pages.
For an arm64 kernel with 64 KiB pages, the default reporting and allocation orders allow entries ranging from 2 MiB to 512 MiB. If the report contains a sufficient number of large entries, we will end up with a descriptor chain that represents over 4 GiB of memory. This violates virtio spec section 2.7.5.2, which requires that the total buffer length of a descriptor chain be at most 4 GiB [1]. A VMM that faithfully follows the spec may stop processing the descriptor chain when it exceeds 4 GiB, and discard less memory than it could. For example, this is the case for the virtio-queue in Rust-VMM [2].
Batch the requests into descriptor chains of at most U32_MAX bytes (4 GiB - 1 byte). We use U32_MAX here so we can conveniently represent the batch length in a u32.
[1] https://docs.oasis-open.org/virtio/virtio/v1.4/virtio-v1.4.html
[2]
https://github.com/rust-vmm/rust-vmm/blob/9f7250b1555ebe022ea4133fda676e6311040738/vm-virtio/virtio-queue/src/chain.rs#L183-L190
---
The wording in the spec is a bit ambiguous because it uses "chain length" for both descriptor counts and the total size of the memory ranges referenced by the descriptors. My read is that the byte limit applies to the sum of descriptor lengths, which is also what rust-vmm checks.
Another option I considered was to limit reporting capacity based on the largest possible block. E.g. for 64 KiB pages, the largest block is 512 MiB, so limit the reporting capacity to 7 to stay below 4GiB. Although simpler, I found that the time to reclaim was quite a bit longer.
I tested this patch on Cloud Hypervisor (which uses Rust-VMM) and QEMU (which doesn't enforce this limit).
drivers/virtio/virtio_balloon.c | 47 ++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 12 deletions(-)
drivers/virtio/virtio_balloon.c +35 -12
@@ -15,6 +15,7 @@#include <linux/slab.h> #include <linux/module.h> #include <linux/balloon.h> +#include <linux/limits.h> #include <linux/oom.h> #include <linux/wait.h> #include <linux/mm.h>
@@ -207,21 +208,43 @@ static int virtballoon_free_page_report(struct page_reporting_dev_info *pr_dev_istruct virtqueue *vq = vb->reporting_vq;unsigned int unused, err; -/* We should always be able to add these buffers to an empty queue. */ - err =virtqueue_add_inbuf (vq, sg, nents, vb,GFP_NOWAIT ); +while (nents) { +bool unmark_end; +struct scatterlist *sg_batch_start = sg; +u32 batch_len =0 ; +unsigned int batch_nents =0 ; -/* -* In the extremely unlikely case that something has occurred and we -* are able to trigger an error we will simply display a warning -* and exit without actually processing the pages. -*/ -if (WARN_ON_ONCE (err)) -return err; +/* +* Virtio spec 2.7.5.2 limits the total buffer length of a +* descriptor chain to 2^32 bytes (4 GiB), so split larger +* reports into batches no bigger than that size. +*/ +while (batch_nents < nents && + sg->length <=U32_MAX - batch_len) { + batch_len += sg->length; + batch_nents +=1 ; + sg =sg_next (sg); + } + unmark_end = !sg_is_last (&sg_batch_start[batch_nents -1 ]); -virtqueue_kick (vq); +if (unmark_end) +sg_mark_end (&sg_batch_start[batch_nents -1 ]); -/* When host has read buffer, this completes via balloon_ack */ -wait_event (vb->acked,virtqueue_get_buf (vq, &unused)); + err =virtqueue_add_inbuf (vq, sg_batch_start, batch_nents, vb, +GFP_NOWAIT ); + +if (unmark_end) +sg_unmark_end (&sg_batch_start[batch_nents -1 ]); + +if (WARN_ON_ONCE (err)) +return err; + +virtqueue_kick (vq); + +/* When host has read buffer, this completes via balloon_ack */ +wait_event (vb->acked,virtqueue_get_buf (vq, &unused)); + nents -= batch_nents; + }return 0 ; }
Signature · 1 line
2.53.0-Meta
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 17 lines
The
virtio_balloondriver reports a scatter-gather list of free pages to the VMM so it may discard unused pages.For an arm64 kernel with 64 KiB pages, the default reporting and allocation orders allow entries ranging from 2 MiB to 512 MiB. If the report contains a sufficient number of large entries, we will end up with a descriptor chain that represents over 4 GiB of memory. This violates virtio spec section 2.7.5.2, which requires that the total buffer length of a descriptor chain be at most 4 GiB [1]. A VMM that faithfully follows the spec may stop processing the descriptor chain when it exceeds 4 GiB, and discard less memory than it could. For example, this is the case for the virtio-queue in Rust-VMM [2].
Batch the requests into descriptor chains of at most
U32_MAXbytes (4 GiB - 1 byte). We useU32_MAXhere so we can conveniently represent the batch length in a u32.
Quoted 6 lines
[1] https://docs.oasis-open.org/virtio/virtio/v1.4/virtio-v1.4.html
[2]
https://github.com/rust-vmm/rust-vmm/blob/9f7250b1555ebe022ea4133fda676e6311040738/vm-virtio/virtio-queue/src/chain.rs#L183-L190Fixes b0c504f15471 ("virtio-balloon: add support for providing free page reports to host")Signed-off-by Ruben Hakobyan <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Michael S. Tsirkin
maintainerGreat patch, thanks!
Quoted 13 lines · Ruben Hakobyan
The
virtio_balloondriver reports a scatter-gather list of free pages to the VMM so it may discard unused pages.For an arm64 kernel with 64 KiB pages, the default reporting and allocation orders allow entries ranging from 2 MiB to 512 MiB. If the report contains a sufficient number of large entries, we will end up with a descriptor chain that represents over 4 GiB of memory. This violates virtio spec section 2.7.5.2, which requires that the total buffer length of a descriptor chain be at most 4 GiB [1]. A VMM that faithfully follows the spec may stop processing the descriptor chain when it exceeds 4 GiB, and discard less memory than it could. For example, this is the case for the virtio-queue in Rust-VMM [2].
Quoted 3 lines
Batch the requests into descriptor chains of at most
U32_MAXbytes (4 GiB - 1 byte). We useU32_MAXhere so we can conveniently represent the batch length in a u32.
It's not just a question of convenient, is it?
Rust is buggy wrt to spec as it allows only U32_MAX.
So it is also to make it work.
So should we maybe fix Rust to be spec compliant and allow 2^32 too?
I think we should definitely recommend U32_MAX in the spec because otherwise it can not fit in the used length.
Quoted 5 lines
[1] https://docs.oasis-open.org/virtio/virtio/v1.4/virtio-v1.4.html
[2]
https://github.com/rust-vmm/rust-vmm/blob/9f7250b1555ebe022ea4133fda676e6311040738/vm-virtio/virtio-queue/src/chain.rs#L183-L190
Quoted 8 lines
Fixes b0c504f15471 ("virtio-balloon: add support for providing free page reports to host")Signed-off-by Ruben Hakobyan <[email protected]>---
The wording in the spec is a bit ambiguous because it uses "chain length" for both descriptor counts and the total size of the memory ranges referenced by the descriptors. My read is that the byte limit applies to the sum of descriptor lengths, which is also what rust-vmm checks.
quote what you are referring to please, also in the commit log not after ---.
I would also CC [email protected] - spec should recommend U32_MAX.
Quoted 6 lines
Another option I considered was to limit reporting capacity based on the largest possible block. E.g. for 64 KiB pages, the largest block is 512 MiB, so limit the reporting capacity to 7 to stay below 4GiB. Although simpler, I found that the time to reclaim was quite a bit longer.
So I was going to apply this actually:
https://lore.kernel.org/all/b541ff170218bd5d45b551b5132e7cf2c490d761.1780906288.git.mst@redhat.com/
which makes the question completely moot.
Does this have any effect on performance?
Thanks!
Quoted 30 lines
I tested this patch on Cloud Hypervisor (which uses Rust-VMM) and QEMU (which doesn't enforce this limit).
drivers/virtio/virtio_balloon.c | 47 ++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 12 deletions(-)drivers/virtio/virtio_balloon.c +6
-2@@ -15,6 +15,7 @@#include <linux/slab.h> #include <linux/module.h> #include <linux/balloon.h> +#include <linux/limits.h> #include <linux/oom.h> #include <linux/wait.h> #include <linux/mm.h> @@ -207,21 +208,43 @@ static int virtballoon_free_page_report(struct page_reporting_dev_info *pr_dev_istruct virtqueue *vq = vb->reporting_vq;unsigned int unused, err; -/* We should always be able to add these buffers to an empty queue. */ - err =virtqueue_add_inbuf (vq, sg, nents, vb,GFP_NOWAIT ); +while (nents) { +bool unmark_end; +struct scatterlist *sg_batch_start = sg; +u32 batch_len =0 ; +unsigned int batch_nents =0 ;
drivers/virtio/virtio_balloon.c static int virtballoon_free_page_report(struct page_reporting_dev_info *pr_dev_i · line 218
Quoted 10 lines
drivers/virtio/virtio_balloon.c +2
-7-/* -* In the extremely unlikely case that something has occurred and we -* are able to trigger an error we will simply display a warning -* and exit without actually processing the pages. -*/ -if (WARN_ON_ONCE (err)) -return err; +/* +* Virtio spec 2.7.5.2
Please add spec version and chapter title, and quote the spec text in question verbatim.
drivers/virtio/virtio_balloon.c static int virtballoon_free_page_report(struct page_reporting_dev_info *pr_dev_i · line 223
Quoted 6 lines
limits the total buffer length of a
drivers/virtio/virtio_balloon.c +5
-0+* descriptor chain to 2^32 bytes (4 GiB), so split larger +* reports into batches no bigger than that size. +*/ +while (batch_nents < nents && + sg->length <=U32_MAX - batch_len) {
here you actually limit to U32_MAX which is 2^32 - 1 as commit log says.
Quoted 32 lines
drivers/virtio/virtio_balloon.c +22
-3+ batch_len += sg->length; + batch_nents +=1 ; + sg =sg_next (sg); + } + unmark_end = !sg_is_last (&sg_batch_start[batch_nents -1 ]); -virtqueue_kick (vq); +if (unmark_end) +sg_mark_end (&sg_batch_start[batch_nents -1 ]); -/* When host has read buffer, this completes via balloon_ack */ -wait_event (vb->acked,virtqueue_get_buf (vq, &unused)); + err =virtqueue_add_inbuf (vq, sg_batch_start, batch_nents, vb, +GFP_NOWAIT ); + +if (unmark_end) +sg_unmark_end (&sg_batch_start[batch_nents -1 ]); + +if (WARN_ON_ONCE (err)) +return err; + +virtqueue_kick (vq); + +/* When host has read buffer, this completes via balloon_ack */ +wait_event (vb->acked,virtqueue_get_buf (vq, &unused)); + nents -= batch_nents; + }return 0 ; }Signature · 1 line
2.53.0-Meta
Quoted 6 lines · Michael S. Tsirkin
Great patch, thanks!
Quoted 2 lines · Ruben Hakobyan
The
virtio_balloondriver reports a scatter-gather list of free pages to the VMM so it may discard unused pages.
...
Quoted 9 lines
It's not just a question of convenient, is it?
Rust is buggy wrt to spec as it allows onlyU32_MAX.
So it is also to make it work.
So should we maybe fix Rust to be spec compliant and allow 2^32 too?I think we should definitely recommend
U32_MAXin the spec because otherwise it can not fit in the used length.
Thanks for the review!
Sounds good to me, I will CC [email protected] as per your recommendation.
Agreed that Rust-VMM should also be fixed in case 2^32 needs to stay the hard limit. I think here for the driver, we can be more conservative as long as we stay below 2^32.
...
Quoted 10 lines
Quoted 6 lines
---
The wording in the spec is a bit ambiguous because it uses "chain length" for both descriptor counts and the total size of the memory ranges referenced by the descriptors. My read is that the byte limit applies to the sum of descriptor lengths, which is also what rust-vmm checks.quote what you are referring to please, also in the commit log not after ---.
I will update with quotes in v2, thanks.
Quoted 15 lines
I would also CC [email protected] - spec should recommend
U32_MAX.Quoted 6 lines
Another option I considered was to limit reporting capacity based on the largest possible block. E.g. for 64 KiB pages, the largest block is 512 MiB, so limit the reporting capacity to 7 to stay below 4GiB. Although simpler, I found that the time to reclaim was quite a bit longer.
So I was going to apply this actually:
Quoted 7 lines
https://lore.kernel.org/all/b541ff170218bd5d45b551b5132e7cf2c490d761.1780906288.git.mst@redhat.com/
which makes the question completely moot.
Does this have any effect on performance?
Thanks!
Thanks for sharing this. Yes, this would also make sure we stay below the limit. Regarding performance, I tested this on QEMU and it calls virtio_notify() after each completion. Using one chain per entry creates more chains, so QEMU can use more CPU time to reclaim the same amount of memory.
If we go with this, I think a QEMU patch to batch notifications could reduce the overhead. Alternatively, I think we could create larger chains here but make sure they stay within the 2^32 limit, and keeping your approach of queuing them all before kicking.
I am happy to share a patch with my thinking here.
...
Quoted 7 lines
drivers/virtio/virtio_balloon.c static int virtballoon_free_page_report(struct page_reporting_dev_info *pr_dev_i
Quoted 4 lines
drivers/virtio/virtio_balloon.c +2
-2-if (WARN_ON_ONCE (err)) -return err; +/* +* Virtio spec 2.7.5.2 Please add spec version and chapter title, and quote the spec text in question verbatim.
Will update, thanks for the note.
Quoted 9 lines
drivers/virtio/virtio_balloon.c static int virtballoon_free_page_report(struct page_reporting_dev_info *pr_dev_i · line 223
Quoted 7 lines
limits the total buffer length of a
drivers/virtio/virtio_balloon.c +5
-0+* descriptor chain to 2^32 bytes (4 GiB), so split larger +* reports into batches no bigger than that size. +*/ +while (batch_nents < nents && + sg->length <=U32_MAX - batch_len) {here you actually limit to
U32_MAXwhich is 2^32 - 1 as commit log says.
I will update the comment above to be more explicit about this.
...
Michael S. Tsirkin
maintainerQuoted 61 lines · Ruben Hakobyan
Quoted 6 lines · Michael S. Tsirkin
Great patch, thanks!
Quoted 2 lines · Ruben Hakobyan
The
virtio_balloondriver reports a scatter-gather list of free pages to the VMM so it may discard unused pages....
Quoted 9 lines
It's not just a question of convenient, is it?
Rust is buggy wrt to spec as it allows onlyU32_MAX.
So it is also to make it work.
So should we maybe fix Rust to be spec compliant and allow 2^32 too?I think we should definitely recommend
U32_MAXin the spec because otherwise it can not fit in the used length.Thanks for the review!
Sounds good to me, I will CC [email protected] as per your recommendation.
Agreed that Rust-VMM should also be fixed in case 2^32 needs to stay the hard limit. I think here for the driver, we can be more conservative as long as we stay below 2^32.
...
Quoted 10 lines
Quoted 6 lines
---
The wording in the spec is a bit ambiguous because it uses "chain length" for both descriptor counts and the total size of the memory ranges referenced by the descriptors. My read is that the byte limit applies to the sum of descriptor lengths, which is also what rust-vmm checks.quote what you are referring to please, also in the commit log not after ---.
I will update with quotes in v2, thanks.
Quoted 19 lines
I would also CC [email protected] - spec should recommend
U32_MAX.Quoted 6 lines
Another option I considered was to limit reporting capacity based on the largest possible block. E.g. for 64 KiB pages, the largest block is 512 MiB, so limit the reporting capacity to 7 to stay below 4GiB. Although simpler, I found that the time to reclaim was quite a bit longer.
So I was going to apply this actually:
https://lore.kernel.org/all/b541ff170218bd5d45b551b5132e7cf2c490d761.1780906288.git.mst@redhat.com/
which makes the question completely moot.
Quoted 7 lines
Quoted 3 lines
Does this have any effect on performance?
Thanks!
Thanks for sharing this. Yes, this would also make sure we stay below the limit. Regarding performance, I tested this on QEMU and it calls
virtio_notify()after each completion.
Who, QEMU does?
Quoted 4 lines
Using one chain per entry
creates more chains, so QEMU can use more CPU time to reclaim the same amount of memory.
Quoted 8 lines
If we go with this, I think a QEMU patch to batch notifications could reduce the overhead. Alternatively, I think we could create larger chains here but make sure they stay within the 2^32 limit, and keeping your approach of queuing them all before kicking.
I am happy to share a patch with my thinking here.
...
Oh. It's just the driver being silly and never disabling callbacks.
pls just add virtqueue_enable_cb_delayed and this will fix it.
Sorry, I just realised the other reply went on to the wrong thread, so just reposting it here.
Quoted 64 lines · Michael S. Tsirkin
Quoted 63 lines · Ruben Hakobyan
Quoted 6 lines · Michael S. Tsirkin
Great patch, thanks!
Quoted 2 lines · Ruben Hakobyan
The
virtio_balloondriver reports a scatter-gather list of free pages to the VMM so it may discard unused pages....
Quoted 9 lines
It's not just a question of convenient, is it?
Rust is buggy wrt to spec as it allows onlyU32_MAX.
So it is also to make it work.
So should we maybe fix Rust to be spec compliant and allow 2^32 too?I think we should definitely recommend
U32_MAXin the spec because otherwise it can not fit in the used length.Thanks for the review!
Sounds good to me, I will CC [email protected] as per your recommendation.
Agreed that Rust-VMM should also be fixed in case 2^32 needs to stay the hard limit. I think here for the driver, we can be more conservative as long as we stay below 2^32.
...
Quoted 10 lines
Quoted 6 lines
---
The wording in the spec is a bit ambiguous because it uses "chain length" for both descriptor counts and the total size of the memory ranges referenced by the descriptors. My read is that the byte limit applies to the sum of descriptor lengths, which is also what rust-vmm checks.quote what you are referring to please, also in the commit log not after ---.
I will update with quotes in v2, thanks.
Quoted 21 lines
I would also CC [email protected] - spec should recommend
U32_MAX.Quoted 6 lines
Another option I considered was to limit reporting capacity based on the largest possible block. E.g. for 64 KiB pages, the largest block is 512 MiB, so limit the reporting capacity to 7 to stay below 4GiB. Although simpler, I found that the time to reclaim was quite a bit longer.
So I was going to apply this actually:
https://lore.kernel.org/all/b541ff170218bd5d45b551b5132e7cf2c490d761.1780906288.git.mst@redhat.com/
which makes the question completely moot.
Does this have any effect on performance?
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
Thanks!
Thanks for sharing this. Yes, this would also make sure we stay below the limit. Regarding performance, I tested this on QEMU and it calls
virtio_notify()after each completion.Who, QEMU does?
Yes QEMU, sorry for the ambiguous wording.
Quoted 10 lines
Quoted 10 lines
Using one chain per entry
creates more chains, so QEMU can use more CPU time to reclaim the same amount of memory.If we go with this, I think a QEMU patch to batch notifications could reduce the overhead. Alternatively, I think we could create larger chains here but make sure they stay within the 2^32 limit, and keeping your approach of queuing them all before kicking.
Quoted 9 lines
Quoted 3 lines
I am happy to share a patch with my thinking here.
...
Oh. It's just the driver being silly and never disabling callbacks.
pls just add
virtqueue_enable_cb_delayedand this will fix it.
Ah great, I tried with virtqueue_enable_cb_delayed() and yeah it largely fixed it.
So regarding the patch in
https://lore.kernel.org/all/b541ff170218bd5d45b551b5132e7cf2c490d761.1780906288.git.mst@redhat.com/
I see it's in a larger series, so just wanted to check if you're planning to send this patch separately? Or let me know if you'd like me to do it. Thanks!
Michael S. Tsirkin
maintainerQuoted 101 lines · Ruben Hakobyan
Sorry, I just realised the other reply went on to the wrong thread, so just reposting it here.
Quoted 64 lines · Michael S. Tsirkin
Quoted 63 lines · Ruben Hakobyan
Quoted 6 lines · Michael S. Tsirkin
Great patch, thanks!
Quoted 2 lines · Ruben Hakobyan
The
virtio_balloondriver reports a scatter-gather list of free pages to the VMM so it may discard unused pages....
Quoted 9 lines
It's not just a question of convenient, is it?
Rust is buggy wrt to spec as it allows onlyU32_MAX.
So it is also to make it work.
So should we maybe fix Rust to be spec compliant and allow 2^32 too?I think we should definitely recommend
U32_MAXin the spec because otherwise it can not fit in the used length.Thanks for the review!
Sounds good to me, I will CC [email protected] as per your recommendation.
Agreed that Rust-VMM should also be fixed in case 2^32 needs to stay the hard limit. I think here for the driver, we can be more conservative as long as we stay below 2^32.
...
Quoted 10 lines
Quoted 6 lines
---
The wording in the spec is a bit ambiguous because it uses "chain length" for both descriptor counts and the total size of the memory ranges referenced by the descriptors. My read is that the byte limit applies to the sum of descriptor lengths, which is also what rust-vmm checks.quote what you are referring to please, also in the commit log not after ---.
I will update with quotes in v2, thanks.
Quoted 21 lines
I would also CC [email protected] - spec should recommend
U32_MAX.Quoted 6 lines
Another option I considered was to limit reporting capacity based on the largest possible block. E.g. for 64 KiB pages, the largest block is 512 MiB, so limit the reporting capacity to 7 to stay below 4GiB. Although simpler, I found that the time to reclaim was quite a bit longer.
So I was going to apply this actually:
https://lore.kernel.org/all/b541ff170218bd5d45b551b5132e7cf2c490d761.1780906288.git.mst@redhat.com/
which makes the question completely moot.
Does this have any effect on performance?
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
Thanks!
Thanks for sharing this. Yes, this would also make sure we stay below the limit. Regarding performance, I tested this on QEMU and it calls
virtio_notify()after each completion.Who, QEMU does?
Yes QEMU, sorry for the ambiguous wording.
Quoted 10 lines
Quoted 10 lines
Using one chain per entry
creates more chains, so QEMU can use more CPU time to reclaim the same amount of memory.If we go with this, I think a QEMU patch to batch notifications could reduce the overhead. Alternatively, I think we could create larger chains here but make sure they stay within the 2^32 limit, and keeping your approach of queuing them all before kicking.
Quoted 9 lines
Quoted 3 lines
I am happy to share a patch with my thinking here.
...
Oh. It's just the driver being silly and never disabling callbacks.
pls just add
virtqueue_enable_cb_delayedand this will fix it.Ah great, I tried with
virtqueue_enable_cb_delayed()and yeah it largely fixed it.
Quoted 6 lines
So regarding the patch in
https://lore.kernel.org/all/b541ff170218bd5d45b551b5132e7cf2c490d761.1780906288.git.mst@redhat.com/I see it's in a larger series, so just wanted to check if you're planning to send this patch separately? Or let me know if you'd like me to do it. Thanks!
Yes please do send it separately. That work stalled, the mm guys said they will have someone look into it, then never did.
Or maybe even simpler, as below. BTW we might want virtqueue_enable_cb_last, instead of delayed will be more efficient for event_idx.
drivers/virtio/virtio_balloon.c +0 -1
@@ -246,22 +246,23 @@ static int virtballoon_free_page_report(struct page_reporting_dev_info *pr_dev_iif (nents) {virtqueue_kick (vq);/* When host has read buffer, this completes via balloon_ack */ for (i =0 ; i < nents; i++) {struct scatterlist *entry;unsigned int used_len;virtqueue_enable_cb_delayed (vq);wait_event (vb->acked, (entry =virtqueue_get_buf (vq, &used_len)));virtqueue_disable_cb (vq);if (used_len == entry->length)set_bit (entry - sg, pr_dev_info->zeroed_bitmap); } }return err; --
MST
Quoted 64 lines · Michael S. Tsirkin
Quoted 63 lines · Ruben Hakobyan
Quoted 6 lines · Michael S. Tsirkin
Great patch, thanks!
Quoted 2 lines · Ruben Hakobyan
The
virtio_balloondriver reports a scatter-gather list of free pages to the VMM so it may discard unused pages....
Quoted 9 lines
It's not just a question of convenient, is it?
Rust is buggy wrt to spec as it allows onlyU32_MAX.
So it is also to make it work.
So should we maybe fix Rust to be spec compliant and allow 2^32 too?I think we should definitely recommend
U32_MAXin the spec because otherwise it can not fit in the used length.Thanks for the review!
Sounds good to me, I will CC [email protected] as per your recommendation.
Agreed that Rust-VMM should also be fixed in case 2^32 needs to stay the hard limit. I think here for the driver, we can be more conservative as long as we stay below 2^32.
...
Quoted 10 lines
Quoted 6 lines
---
The wording in the spec is a bit ambiguous because it uses "chain length" for both descriptor counts and the total size of the memory ranges referenced by the descriptors. My read is that the byte limit applies to the sum of descriptor lengths, which is also what rust-vmm checks.quote what you are referring to please, also in the commit log not after ---.
I will update with quotes in v2, thanks.
Quoted 21 lines
I would also CC [email protected] - spec should recommend
U32_MAX.Quoted 6 lines
Another option I considered was to limit reporting capacity based on the largest possible block. E.g. for 64 KiB pages, the largest block is 512 MiB, so limit the reporting capacity to 7 to stay below 4GiB. Although simpler, I found that the time to reclaim was quite a bit longer.
So I was going to apply this actually:
https://lore.kernel.org/all/b541ff170218bd5d45b551b5132e7cf2c490d761.1780906288.git.mst@redhat.com/
which makes the question completely moot.
Does this have any effect on performance?
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
Thanks!
Thanks for sharing this. Yes, this would also make sure we stay below the limit. Regarding performance, I tested this on QEMU and it calls
virtio_notify()after each completion.Who, QEMU does?
Yes QEMU, sorry for the ambiguous wording.
Quoted 10 lines
Quoted 10 lines
Using one chain per entry
creates more chains, so QEMU can use more CPU time to reclaim the same amount of memory.If we go with this, I think a QEMU patch to batch notifications could reduce the overhead. Alternatively, I think we could create larger chains here but make sure they stay within the 2^32 limit, and keeping your approach of queuing them all before kicking.
Quoted 9 lines
Quoted 3 lines
I am happy to share a patch with my thinking here.
...
Oh. It's just the driver being silly and never disabling callbacks.
pls just add
virtqueue_enable_cb_delayedand this will fix it.
Ah great, I tried with virtqueue_enable_cb_delayed() and yeah it largely fixed it.
So regarding the patch in
https://lore.kernel.org/all/b541ff170218bd5d45b551b5132e7cf2c490d761.1780906288.git.mst@redhat.com/
I see it's in a larger series, so just wanted to check if you're planning to send this patch separately? Or let me know if you'd like me to do it. Thanks!
Michael S. Tsirkin
maintainerQuoted 17 lines · Ruben Hakobyan
The
virtio_balloondriver reports a scatter-gather list of free pages to the VMM so it may discard unused pages.For an arm64 kernel with 64 KiB pages, the default reporting and allocation orders allow entries ranging from 2 MiB to 512 MiB. If the report contains a sufficient number of large entries, we will end up with a descriptor chain that represents over 4 GiB of memory. This violates virtio spec section 2.7.5.2, which requires that the total buffer length of a descriptor chain be at most 4 GiB [1]. A VMM that faithfully follows the spec may stop processing the descriptor chain when it exceeds 4 GiB, and discard less memory than it could. For example, this is the case for the virtio-queue in Rust-VMM [2].
Batch the requests into descriptor chains of at most
U32_MAXbytes (4 GiB - 1 byte). We useU32_MAXhere so we can conveniently represent the batch length in a u32.
Quoted 7 lines
[1] https://docs.oasis-open.org/virtio/virtio/v1.4/virtio-v1.4.html
[2]
https://github.com/rust-vmm/rust-vmm/blob/9f7250b1555ebe022ea4133fda676e6311040738/vm-virtio/virtio-queue/src/chain.rs#L183-L190Fixes b0c504f15471 ("virtio-balloon: add support for providing free page reports to host")Signed-off-by Ruben Hakobyan <[email protected]>---
thanks a lot for the report. I posted a patchset for your consideration. if you test it, pls let us know the results. if you see a need for more changes, pls feel free to take over the work on it.
Quoted 88 lines
The wording in the spec is a bit ambiguous because it uses "chain length" for both descriptor counts and the total size of the memory ranges referenced by the descriptors. My read is that the byte limit applies to the sum of descriptor lengths, which is also what rust-vmm checks.
Another option I considered was to limit reporting capacity based on the largest possible block. E.g. for 64 KiB pages, the largest block is 512 MiB, so limit the reporting capacity to 7 to stay below 4GiB. Although simpler, I found that the time to reclaim was quite a bit longer.
I tested this patch on Cloud Hypervisor (which uses Rust-VMM) and QEMU (which doesn't enforce this limit).
drivers/virtio/virtio_balloon.c | 47 ++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 12 deletions(-)drivers/virtio/virtio_balloon.c +35
-12@@ -15,6 +15,7 @@#include <linux/slab.h> #include <linux/module.h> #include <linux/balloon.h> +#include <linux/limits.h> #include <linux/oom.h> #include <linux/wait.h> #include <linux/mm.h> @@ -207,21 +208,43 @@ static int virtballoon_free_page_report(struct page_reporting_dev_info *pr_dev_istruct virtqueue *vq = vb->reporting_vq;unsigned int unused, err; -/* We should always be able to add these buffers to an empty queue. */ - err =virtqueue_add_inbuf (vq, sg, nents, vb,GFP_NOWAIT ); +while (nents) { +bool unmark_end; +struct scatterlist *sg_batch_start = sg; +u32 batch_len =0 ; +unsigned int batch_nents =0 ; -/* -* In the extremely unlikely case that something has occurred and we -* are able to trigger an error we will simply display a warning -* and exit without actually processing the pages. -*/ -if (WARN_ON_ONCE (err)) -return err; +/* +* Virtio spec 2.7.5.2 limits the total buffer length of a +* descriptor chain to 2^32 bytes (4 GiB), so split larger +* reports into batches no bigger than that size. +*/ +while (batch_nents < nents && + sg->length <=U32_MAX - batch_len) { + batch_len += sg->length; + batch_nents +=1 ; + sg =sg_next (sg); + } + unmark_end = !sg_is_last (&sg_batch_start[batch_nents -1 ]); -virtqueue_kick (vq); +if (unmark_end) +sg_mark_end (&sg_batch_start[batch_nents -1 ]); -/* When host has read buffer, this completes via balloon_ack */ -wait_event (vb->acked,virtqueue_get_buf (vq, &unused)); + err =virtqueue_add_inbuf (vq, sg_batch_start, batch_nents, vb, +GFP_NOWAIT ); + +if (unmark_end) +sg_unmark_end (&sg_batch_start[batch_nents -1 ]); + +if (WARN_ON_ONCE (err)) +return err; + +virtqueue_kick (vq); + +/* When host has read buffer, this completes via balloon_ack */ +wait_event (vb->acked,virtqueue_get_buf (vq, &unused)); + nents -= batch_nents; + }return 0 ; }Signature · 1 line
2.53.0-Meta