virtio_balloon: virtio 1 support
Subsystems (4)
Michael S. Tsirkin
maintainerv3 cover virtio_balloon: virtio 1 support
Virtio 1.0 doesn't include a modern balloon device. At some point we'll likely define an incompatible interface with a different ID and different semantics. But for now, it's not a big effort to support a transitional balloon device: this has the advantage of supporting existing drivers, transparently, as well as transports that don't allow mixing virtio 0 and virtio 1 devices. And balloon is an easy device to test, so it's also useful for people to test virtio core handling of transitional devices.
The only interface issue is with the stats buffer, which has misaligned fields. We could leave it as is, but this sets a bad precedent that others might copy by mistake.
As we need to change stats code to do byteswaps for virtio 1.0, it seems easy to fix by prepending a 6 byte reserved field. I also had to change config structure field types from __le32 to __u32 to match other devices. This means we need a couple of __force tags for legacy path but that seems minor.
changes from v2:
fix up stats endian-ness
Changes from v1:
correctly handle config space endian-ness
Michael S. Tsirkin (6):
virtio_balloon: transitional interface
virtio: balloon might not be a legacy device
virtio_ccw: support non-legacy balloon devices
virtio_mmio: support non-legacy balloon devices
virtio_pci: support non-legacy balloon devices
virtio: drop virtio_device_is_legacy_only
include/linux/virtio.h | 2 -- include/uapi/linux/virtio_balloon.h | 11 +++++++++-- drivers/s390/kvm/virtio_ccw.c | 10 +++------- drivers/virtio/virtio.c | 6 ------ drivers/virtio/virtio_balloon.c | 33 +++++++++++++++++++++++---------- drivers/virtio/virtio_mmio.c | 8 -------- drivers/virtio/virtio_pci_modern.c | 3 --- 7 files changed, 35 insertions(+), 38 deletions(-)
Signature · 1 line
MST
Michael S. Tsirkin
maintainerPATCH v3 1/6 virtio_balloon: transitional interface
Virtio 1.0 doesn't include a modern balloon device. But it's not a big change to support a transitional balloon device: this has the advantage of supporting existing drivers, transparently.
--- include/uapi/linux/virtio_balloon.h | 11 ++++++++-- drivers/virtio/virtio_balloon.c | 43 +++++++++++++++++++++++++++++-------- 2 files changed, 43 insertions(+), 11 deletions(-)
include/uapi/linux/virtio_balloon.h +9 -2
@@ -25,6 +25,7 @@* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ +#include <linux/types.h> #include <linux/virtio_ids.h> #include <linux/virtio_config.h>
@@ -38,9 +39,9 @@struct virtio_balloon_config {/* Number of pages host wants Guest to give up. */ -__le32 num_pages; +__u32 num_pages;/* Number of pages we've actually got in balloon. */ -__le32 actual; +__u32 actual; };#define VIRTIO_BALLOON_S_SWAP_IN 0/* Amount of memory swapped in */
@@ -56,4 +57,10 @@ struct virtio_balloon_stat {__u64 val; } __attribute__((packed)); +struct virtio_balloon_stat_modern { +__le16 tag; +__u8 reserved[6 ]; +__le64 val; +}; +#endif /* _LINUX_VIRTIO_BALLOON_H */
drivers/virtio/virtio_balloon.c +34 -9
87 lines not on this page · raw message
Signature · 1 line
MST
Michael S. Tsirkin
maintainerRe: [PATCH v3 1/6] virtio_balloon: transitional interface
Quoted 10 lines · Michael S. Tsirkin
Virtio 1.0 doesn't include a modern balloon device. But it's not a big change to support a transitional balloon device: this has the advantage of supporting existing drivers, transparently.
Signed-off-by Michael S. Tsirkin <[email protected]>--- include/uapi/linux/virtio_balloon.h | 11 ++++++++-- drivers/virtio/virtio_balloon.c | 43 +++++++++++++++++++++++++++++-------- 2 files changed, 43 insertions(+), 11 deletions(-)
So all in all 32 LOC added, and 9 out of these
deal with endian-ness differences.
Seems like a small cost for guests to pay for a clean spec, no?
Signature · 1 line
MST
Re: [PATCH v3 1/6] virtio_balloon: transitional interface
Quoted 12 lines · On Wed, 1 Apr 2015 12:49:02 +0200 "Michael S. Tsirkin" <[email protected]> wrote:
Quoted 10 lines · Michael S. Tsirkin
Virtio 1.0 doesn't include a modern balloon device. But it's not a big change to support a transitional balloon device: this has the advantage of supporting existing drivers, transparently.
Signed-off-by Michael S. Tsirkin <[email protected]>--- include/uapi/linux/virtio_balloon.h | 11 ++++++++-- drivers/virtio/virtio_balloon.c | 43 +++++++++++++++++++++++++++++-------- 2 files changed, 43 insertions(+), 11 deletions(-)
Quoted 5 lines
So all in all 32 LOC added, and 9 out of these
deal with endian-ness differences.Seems like a small cost for guests to pay for a clean spec, no?
I'm not opposed to this per se, but I'm not totally convinced of the usefulness :)
Seeing the qemu side would be helpful.
Michael S. Tsirkin
maintainerRe: [PATCH v3 1/6] virtio_balloon: transitional interface
Quoted 18 lines · Cornelia Huck
Quoted 15 lines · On Wed, 1 Apr 2015 12:49:02 +0200 "Michael S. Tsirkin"…
Quoted 10 lines · Michael S. Tsirkin
Virtio 1.0 doesn't include a modern balloon device. But it's not a big change to support a transitional balloon device: this has the advantage of supporting existing drivers, transparently.
Signed-off-by Michael S. Tsirkin <[email protected]>--- include/uapi/linux/virtio_balloon.h | 11 ++++++++-- drivers/virtio/virtio_balloon.c | 43 +++++++++++++++++++++++++++++-------- 2 files changed, 43 insertions(+), 11 deletions(-)So all in all 32 LOC added, and 9 out of these
deal with endian-ness differences.
Quoted 7 lines
Quoted 2 lines
Seems like a small cost for guests to pay for a clean spec, no?
I'm not opposed to this per se, but I'm not totally convinced of the usefulness :)
Seeing the qemu side would be helpful.
Cleaning this up for submission, should be ready RSN.
Signature · 1 line
MST
Michael S. Tsirkin
maintainerRe: [PATCH v3 1/6] virtio_balloon: transitional interface
In reply to Cornelia HuckQuoted 18 lines · Cornelia Huck
Quoted 15 lines · On Wed, 1 Apr 2015 12:49:02 +0200 "Michael S. Tsirkin" <[email protected]> wrote:
Quoted 10 lines · Michael S. Tsirkin
Virtio 1.0 doesn't include a modern balloon device. But it's not a big change to support a transitional balloon device: this has the advantage of supporting existing drivers, transparently.
Signed-off-by Michael S. Tsirkin <[email protected]>--- include/uapi/linux/virtio_balloon.h | 11 ++++++++-- drivers/virtio/virtio_balloon.c | 43 +++++++++++++++++++++++++++++-------- 2 files changed, 43 insertions(+), 11 deletions(-)So all in all 32 LOC added, and 9 out of these
deal with endian-ness differences.
Quoted 7 lines
Quoted 2 lines
Seems like a small cost for guests to pay for a clean spec, no?
I'm not opposed to this per se, but I'm not totally convinced of the usefulness :)
Seeing the qemu side would be helpful.
Cleaning this up for submission, should be ready RSN.
Signature · 1 line
MST
Re: [PATCH v3 1/6] virtio_balloon: transitional interface
In reply to Michael S. TsirkinQuoted 12 lines · On Wed, 1 Apr 2015 12:49:02 +0200 "Michael S. Tsirkin" <[email protected]> wrote:
Quoted 10 lines · Michael S. Tsirkin
Virtio 1.0 doesn't include a modern balloon device. But it's not a big change to support a transitional balloon device: this has the advantage of supporting existing drivers, transparently.
Signed-off-by Michael S. Tsirkin <[email protected]>--- include/uapi/linux/virtio_balloon.h | 11 ++++++++-- drivers/virtio/virtio_balloon.c | 43 +++++++++++++++++++++++++++++-------- 2 files changed, 43 insertions(+), 11 deletions(-)
Quoted 5 lines
So all in all 32 LOC added, and 9 out of these
deal with endian-ness differences.Seems like a small cost for guests to pay for a clean spec, no?
I'm not opposed to this per se, but I'm not totally convinced of the usefulness :)
Seeing the qemu side would be helpful.
Michael S. Tsirkin
maintainerPATCH v3 1/6 virtio_balloon: transitional interface
In reply to Michael S. TsirkinVirtio 1.0 doesn't include a modern balloon device. But it's not a big change to support a transitional balloon device: this has the advantage of supporting existing drivers, transparently.
--- include/uapi/linux/virtio_balloon.h | 11 ++++++++-- drivers/virtio/virtio_balloon.c | 43 +++++++++++++++++++++++++++++-------- 2 files changed, 43 insertions(+), 11 deletions(-)
include/uapi/linux/virtio_balloon.h +9 -2
@@ -25,6 +25,7 @@* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ +#include <linux/types.h> #include <linux/virtio_ids.h> #include <linux/virtio_config.h>
@@ -38,9 +39,9 @@struct virtio_balloon_config {/* Number of pages host wants Guest to give up. */ -__le32 num_pages; +__u32 num_pages;/* Number of pages we've actually got in balloon. */ -__le32 actual; +__u32 actual; };#define VIRTIO_BALLOON_S_SWAP_IN 0/* Amount of memory swapped in */
@@ -56,4 +57,10 @@ struct virtio_balloon_stat {__u64 val; } __attribute__((packed)); +struct virtio_balloon_stat_modern { +__le16 tag; +__u8 reserved[6 ]; +__le64 val; +}; +#endif /* _LINUX_VIRTIO_BALLOON_H */
drivers/virtio/virtio_balloon.c +34 -9
87 lines not on this page · raw message
Signature · 1 line
MST
Michael S. Tsirkin
maintainerPATCH v3 2/6 virtio: balloon might not be a legacy device
In reply to Michael S. TsirkinWe added transitional device support to balloon driver, so we don't need to black-list it in core anymore.
--- drivers/virtio/virtio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/virtio/virtio.c +1 -1
@@ -280,7 +280,7 @@ static struct bus_type virtio_bus = {bool virtio_device_is_legacy_only (struct virtio_device_id id) { -return id.device ==VIRTIO_ID_BALLOON ; +return false; }EXPORT_SYMBOL_GPL (virtio_device_is_legacy_only);
Signature · 1 line
MST
Michael S. Tsirkin
maintainerPATCH v3 2/6 virtio: balloon might not be a legacy device
In reply to Michael S. TsirkinWe added transitional device support to balloon driver, so we don't need to black-list it in core anymore.
--- drivers/virtio/virtio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/virtio/virtio.c +1 -1
@@ -280,7 +280,7 @@ static struct bus_type virtio_bus = {bool virtio_device_is_legacy_only (struct virtio_device_id id) { -return id.device ==VIRTIO_ID_BALLOON ; +return false; }EXPORT_SYMBOL_GPL (virtio_device_is_legacy_only);
Signature · 1 line
MST
Re: [PATCH v3 2/6] virtio: balloon might not be a legacy device
Quoted 8 lines · On Wed, 1 Apr 2015 12:35:48 +0200 "Michael S. Tsirkin" <[email protected]> wrote:
We added transitional device support to balloon driver, so we don't need to black-list it in core anymore.
Signed-off-by Michael S. Tsirkin <[email protected]>--- drivers/virtio/virtio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Michael S. Tsirkin
maintainerPATCH v3 3/6 virtio_ccw: support non-legacy balloon devices
In reply to Michael S. Tsirkinvirtio_device_is_legacy_only is always false now,
drop the test from virtio ccw.
--- drivers/s390/kvm/virtio_ccw.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/s390/kvm/virtio_ccw.c +3 -7
@@ -1201,13 +1201,9 @@ static int virtio_ccw_online(struct ccw_device *cdev) vcdev->vdev.id.vendor = cdev->id.cu_type; vcdev->vdev.id.device = cdev->id.cu_model; -if (virtio_device_is_legacy_only (vcdev->vdev.id)) { - vcdev->revision =0 ; - }else { - ret =virtio_ccw_set_transport_rev (vcdev); -if (ret) - goto out_free; - } + ret =virtio_ccw_set_transport_rev (vcdev); +if (ret) + goto out_free; ret =register_virtio_device (&vcdev->vdev);if (ret) {
Signature · 1 line
MST
Re: [virtio-dev] [PATCH v3 3/6] virtio_ccw: support non-legacy balloon devices
Am 01.04.2015 um 12:35 schrieb Michael S. Tsirkin:
Quoted 2 lines
virtio_device_is_legacy_onlyis always false now,
drop the test from virtio ccw.
Can you add the commit subject of patch2 here as a prereq for this patch? This will hopefully avoid backport issues on distros that want to take this patch but not the other for whatever reasons.
this patch is then.
Quoted 27 lines
Signed-off-by Michael S. Tsirkin <[email protected]>--- drivers/s390/kvm/virtio_ccw.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-)drivers/s390/kvm/virtio_ccw.c +3
-7@@ -1201,13 +1201,9 @@ static int virtio_ccw_online(struct ccw_device *cdev) vcdev->vdev.id.vendor = cdev->id.cu_type; vcdev->vdev.id.device = cdev->id.cu_model; -if (virtio_device_is_legacy_only (vcdev->vdev.id)) { - vcdev->revision =0 ; - }else { - ret =virtio_ccw_set_transport_rev (vcdev); -if (ret) - goto out_free; - } + ret =virtio_ccw_set_transport_rev (vcdev); +if (ret) + goto out_free; ret =register_virtio_device (&vcdev->vdev);if (ret) {
Cornelia Huck
Reviewed-byRe: [virtio-dev] [PATCH v3 3/6] virtio_ccw: support non-legacy balloon devices
Quoted 8 lines · On Wed, 01 Apr 2015 12:40:10 +0200 Christian Borntraeger…
Am 01.04.2015 um 12:35 schrieb Michael S. Tsirkin:
Quoted 2 lines
virtio_device_is_legacy_onlyis always false now,
drop the test from virtio ccw.Can you add the commit subject of patch2 here as a prereq for this patch? This will hopefully avoid backport issues on distros that want to take this patch but not the other for whatever reasons.
Seconded.
Quoted 4 lines
this patch is then.
Reviewed-by Christian Borntraeger <[email protected]>
Quoted 6 lines
Quoted 6 lines
Signed-off-by Michael S. Tsirkin <[email protected]>--- drivers/s390/kvm/virtio_ccw.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-)
Michael, I assume you would take this patch through your tree?
Re: [virtio-dev] [PATCH v3 3/6] virtio_ccw: support non-legacy balloon devices
In reply to Christian BorntraegerQuoted 8 lines · Christian Borntraeger
Am 01.04.2015 um 12:35 schrieb Michael S. Tsirkin:
Quoted 2 lines
virtio_device_is_legacy_onlyis always false now,
drop the test from virtio ccw.Can you add the commit subject of patch2 here as a prereq for this patch? This will hopefully avoid backport issues on distros that want to take this patch but not the other for whatever reasons.
I changed commit wording to:
As of last patch, virtio_device_is_legacy_only is always false, drop the test from virtio ccw.
That should be clear to anyone seeking -stable patches.
Thanks,
Rusty.
Michael S. Tsirkin
maintainerPATCH v3 4/6 virtio_mmio: support non-legacy balloon devices
In reply to Michael S. Tsirkinvirtio_device_is_legacy_only is always false now,
drop the test from virtio mmio.
--- drivers/virtio/virtio_mmio.c | 8 -------- 1 file changed, 8 deletions(-)
drivers/virtio/virtio_mmio.c +0 -8
@@ -581,14 +581,6 @@ static int virtio_mmio_probe(struct platform_device *pdev) } vm_dev->vdev.id.vendor =readl (vm_dev->base +VIRTIO_MMIO_VENDOR_ID ); -/* Reject legacy-only IDs for version 2 devices */ -if (vm_dev->version ==2 && -virtio_device_is_legacy_only (vm_dev->vdev.id)) { -dev_err (&pdev->dev,"Version 2 not supported for devices %u!\n" , - vm_dev->vdev.id.device); -return -ENODEV ; - } -if (vm_dev->version ==1 )writel (PAGE_SIZE , vm_dev->base +VIRTIO_MMIO_GUEST_PAGE_SIZE );
Signature · 1 line
MST
Michael S. Tsirkin
maintainerPATCH v3 4/6 virtio_mmio: support non-legacy balloon devices
In reply to Michael S. Tsirkinvirtio_device_is_legacy_only is always false now,
drop the test from virtio mmio.
--- drivers/virtio/virtio_mmio.c | 8 -------- 1 file changed, 8 deletions(-)
drivers/virtio/virtio_mmio.c +0 -8
@@ -581,14 +581,6 @@ static int virtio_mmio_probe(struct platform_device *pdev) } vm_dev->vdev.id.vendor =readl (vm_dev->base +VIRTIO_MMIO_VENDOR_ID ); -/* Reject legacy-only IDs for version 2 devices */ -if (vm_dev->version ==2 && -virtio_device_is_legacy_only (vm_dev->vdev.id)) { -dev_err (&pdev->dev,"Version 2 not supported for devices %u!\n" , - vm_dev->vdev.id.device); -return -ENODEV ; - } -if (vm_dev->version ==1 )writel (PAGE_SIZE , vm_dev->base +VIRTIO_MMIO_GUEST_PAGE_SIZE );
Signature · 1 line
MST
Michael S. Tsirkin
maintainerPATCH v3 5/6 virtio_pci: support non-legacy balloon devices
In reply to Michael S. Tsirkinvirtio_device_is_legacy_only is always false now,
drop the test from virtio pci.
--- drivers/virtio/virtio_pci_modern.c | 3 --- 1 file changed, 3 deletions(-)
drivers/virtio/virtio_pci_modern.c +0 -3
@@ -577,9 +577,6 @@ int virtio_pci_modern_probe(struct virtio_pci_device *vp_dev) } vp_dev->vdev.id.vendor = pci_dev->subsystem_vendor; -if (virtio_device_is_legacy_only (vp_dev->vdev.id)) -return -ENODEV ; -/* check for a common config: if not, use legacy mode (bar 0). */ common =virtio_pci_find_capability (pci_dev,VIRTIO_PCI_CAP_COMMON_CFG ,IORESOURCE_IO |IORESOURCE_MEM );
Signature · 1 line
MST
Michael S. Tsirkin
maintainerPATCH v3 5/6 virtio_pci: support non-legacy balloon devices
In reply to Michael S. Tsirkinvirtio_device_is_legacy_only is always false now,
drop the test from virtio pci.
--- drivers/virtio/virtio_pci_modern.c | 3 --- 1 file changed, 3 deletions(-)
drivers/virtio/virtio_pci_modern.c +0 -3
@@ -577,9 +577,6 @@ int virtio_pci_modern_probe(struct virtio_pci_device *vp_dev) } vp_dev->vdev.id.vendor = pci_dev->subsystem_vendor; -if (virtio_device_is_legacy_only (vp_dev->vdev.id)) -return -ENODEV ; -/* check for a common config: if not, use legacy mode (bar 0). */ common =virtio_pci_find_capability (pci_dev,VIRTIO_PCI_CAP_COMMON_CFG ,IORESOURCE_IO |IORESOURCE_MEM );
Signature · 1 line
MST
Michael S. Tsirkin
maintainerPATCH v3 6/6 virtio: drop virtio_device_is_legacy_only
In reply to Michael S. Tsirkinvirtio_device_is_legacy_only is now unused, drop
it from core.
--- include/linux/virtio.h | 2 -- drivers/virtio/virtio.c | 6 ------ 2 files changed, 8 deletions(-)
include/linux/virtio.h +0 -2
@@ -108,8 +108,6 @@ struct virtio_device {void *priv; }; -bool virtio_device_is_legacy_only (struct virtio_device_id id); -static inline struct virtio_device *dev_to_virtio (struct device *_dev) { return container_of(_dev, struct virtio_device, dev);
drivers/virtio/virtio.c +0 -6
@@ -278,12 +278,6 @@ static struct bus_type virtio_bus = { .remove = virtio_dev_remove, }; -bool virtio_device_is_legacy_only (struct virtio_device_id id) -{ -return false; -} -EXPORT_SYMBOL_GPL (virtio_device_is_legacy_only); -int register_virtio_driver (struct virtio_driver *driver) {/* Catch this early. */
Signature · 1 line
MST
Re: [PATCH v3 6/6] virtio: drop virtio_device_is_legacy_only
Quoted 8 lines · On Wed, 1 Apr 2015 12:36:03 +0200 "Michael S. Tsirkin" <[email protected]> wrote:
virtio_device_is_legacy_onlyis now unused, drop
it from core.Signed-off-by Michael S. Tsirkin <[email protected]>--- include/linux/virtio.h | 2 -- drivers/virtio/virtio.c | 6 ------ 2 files changed, 8 deletions(-)
Re: [PATCH v3 6/6] virtio: drop virtio_device_is_legacy_only
In reply to Michael S. TsirkinQuoted 8 lines · On Wed, 1 Apr 2015 12:36:03 +0200 "Michael S. Tsirkin" <[email protected]> wrote:
virtio_device_is_legacy_onlyis now unused, drop
it from core.Signed-off-by Michael S. Tsirkin <[email protected]>--- include/linux/virtio.h | 2 -- drivers/virtio/virtio.c | 6 ------ 2 files changed, 8 deletions(-)
Michael S. Tsirkin
maintainerQuoted 8 lines · Michael S. Tsirkin
Virtio 1.0 doesn't include a modern balloon device. At some point we'll likely define an incompatible interface with a different ID and different semantics. But for now, it's not a big effort to support a transitional balloon device: this has the advantage of supporting existing drivers, transparently, as well as transports that don't allow mixing virtio 0 and virtio 1 devices. And balloon is an easy device to test, so it's also useful for people to test virtio core handling of transitional devices.
Quoted 8 lines
The only interface issue is with the stats buffer, which has misaligned fields. We could leave it as is, but this sets a bad precedent that others might copy by mistake.
As we need to change stats code to do byteswaps for virtio 1.0, it seems easy to fix by prepending a 6 byte reserved field. I also had to change config structure field types from
__le32to__u32to match other devices. This means we need a couple of__forcetags for legacy path but that seems minor.
Rusty, what are your thoughts here?
How about merging this for 4.1?
Quoted 24 lines
changes from v2:
fix up stats endian-ness
Changes from v1:
correctly handle config space endian-nessMichael S. Tsirkin (6):
virtio_balloon: transitional interface
virtio: balloon might not be a legacy device
virtio_ccw: support non-legacy balloon devices
virtio_mmio: support non-legacy balloon devices
virtio_pci: support non-legacy balloon devices
virtio: dropvirtio_device_is_legacy_onlyinclude/linux/virtio.h | 2 -- include/uapi/linux/virtio_balloon.h | 11 +++++++++-- drivers/s390/kvm/virtio_ccw.c | 10 +++------- drivers/virtio/virtio.c | 6 ------ drivers/virtio/virtio_balloon.c | 33 +++++++++++++++++++++++---------- drivers/virtio/virtio_mmio.c | 8 -------- drivers/virtio/virtio_pci_modern.c | 3 --- 7 files changed, 35 insertions(+), 38 deletions(-)Signature · 1 line
MST
Quoted 13 lines · Michael S. Tsirkin
Quoted 12 lines · Michael S. Tsirkin
Virtio 1.0 doesn't include a modern balloon device. At some point we'll likely define an incompatible interface with a different ID and different semantics. But for now, it's not a big effort to support a transitional balloon device: this has the advantage of supporting existing drivers, transparently, as well as transports that don't allow mixing virtio 0 and virtio 1 devices. And balloon is an easy device to test, so it's also useful for people to test virtio core handling of transitional devices.
The only interface issue is with the stats buffer, which has misaligned fields. We could leave it as is, but this sets a bad precedent that others might copy by mistake.
Quoted 7 lines
Quoted 4 lines
As we need to change stats code to do byteswaps for virtio 1.0, it seems easy to fix by prepending a 6 byte reserved field. I also had to change config structure field types from
__le32to__u32to match other devices. This means we need a couple of__forcetags for legacy path but that seems minor.Rusty, what are your thoughts here?
How about merging this for 4.1?
I disagree with making any changes, other than allowing it to be used with VIRTIO_F_VERSION_1.
However it doesn't seem to bother anyone else, so I've applied it for 4.1.
Thanks,
Rusty.
Quoted 13 lines · Michael S. Tsirkin
Quoted 12 lines · Michael S. Tsirkin
Virtio 1.0 doesn't include a modern balloon device. At some point we'll likely define an incompatible interface with a different ID and different semantics. But for now, it's not a big effort to support a transitional balloon device: this has the advantage of supporting existing drivers, transparently, as well as transports that don't allow mixing virtio 0 and virtio 1 devices. And balloon is an easy device to test, so it's also useful for people to test virtio core handling of transitional devices.
The only interface issue is with the stats buffer, which has misaligned fields. We could leave it as is, but this sets a bad precedent that others might copy by mistake.
Quoted 7 lines
Quoted 4 lines
As we need to change stats code to do byteswaps for virtio 1.0, it seems easy to fix by prepending a 6 byte reserved field. I also had to change config structure field types from
__le32to__u32to match other devices. This means we need a couple of__forcetags for legacy path but that seems minor.Rusty, what are your thoughts here?
How about merging this for 4.1?
I disagree with making any changes, other than allowing it to be used with VIRTIO_F_VERSION_1.
However it doesn't seem to bother anyone else, so I've applied it for 4.1.
Thanks,
Rusty.
Quoted 19 lines · On Tue, 14 Apr 2015 10:42:56 +0930 Rusty Russell <[email protected]> wrote:
Quoted 18 lines · Michael S. Tsirkin
Quoted 16 lines · Michael S. Tsirkin
Virtio 1.0 doesn't include a modern balloon device. At some point we'll likely define an incompatible interface with a different ID and different semantics. But for now, it's not a big effort to support a transitional balloon device: this has the advantage of supporting existing drivers, transparently, as well as transports that don't allow mixing virtio 0 and virtio 1 devices. And balloon is an easy device to test, so it's also useful for people to test virtio core handling of transitional devices.
The only interface issue is with the stats buffer, which has misaligned fields. We could leave it as is, but this sets a bad precedent that others might copy by mistake.
As we need to change stats code to do byteswaps for virtio 1.0, it seems easy to fix by prepending a 6 byte reserved field. I also had to change config structure field types from
__le32to__u32to match other devices. This means we need a couple of__forcetags for legacy path but that seems minor.
Quoted 8 lines
Quoted 2 lines
Rusty, what are your thoughts here?
How about merging this for 4.1?I disagree with making any changes, other than allowing it to be used with
VIRTIO_F_VERSION_1.However it doesn't seem to bother anyone else, so I've applied it for 4.1.
I'm still not really convinced about the stats change either, FWIW. Still time to reconsider? And should we perhaps wait with merging until the spec change allowing version 1 has been accepted?
Michael S. Tsirkin
maintainerQuoted 28 lines · Cornelia Huck
Quoted 25 lines · On Tue, 14 Apr 2015 10:42:56 +0930 Rusty Russell <[email protected]> wrote:
Quoted 13 lines · Michael S. Tsirkin
Quoted 12 lines · Michael S. Tsirkin
Virtio 1.0 doesn't include a modern balloon device. At some point we'll likely define an incompatible interface with a different ID and different semantics. But for now, it's not a big effort to support a transitional balloon device: this has the advantage of supporting existing drivers, transparently, as well as transports that don't allow mixing virtio 0 and virtio 1 devices. And balloon is an easy device to test, so it's also useful for people to test virtio core handling of transitional devices.
The only interface issue is with the stats buffer, which has misaligned fields. We could leave it as is, but this sets a bad precedent that others might copy by mistake.
Quoted 7 lines
Quoted 4 lines
As we need to change stats code to do byteswaps for virtio 1.0, it seems easy to fix by prepending a 6 byte reserved field. I also had to change config structure field types from
__le32to__u32to match other devices. This means we need a couple of__forcetags for legacy path but that seems minor.Rusty, what are your thoughts here?
How about merging this for 4.1?I disagree with making any changes, other than allowing it to be used with
VIRTIO_F_VERSION_1.
Quoted 8 lines
Quoted 2 lines
However it doesn't seem to bother anyone else, so I've applied it for 4.1.
I'm still not really convinced about the stats change either, FWIW. Still time to reconsider?
And should we perhaps wait with merging until the spec change allowing version 1 has been accepted?
That's not how we did this historically: we require all parts (spec,qemu,linux) to be available, but don't create specific order between them. In particular, I'd strongly prefer not waiting until 4.2, that would interfere with putting virtio 1 out to use in the field.
Since both Rusty and Cornelia are against virtio_balloon_stat_modern, I accept this as the majority decision. And switching
over to __virtio tags found a bug, so I'm convinced now.
--->
virtio_balloon: drop virtio_balloon_stat_modern
Looks like we are better off sticking with the misaligned stat struct, to reduce the amount of virtio 1 specific code in balloon. So let's do it.
Add a detailed comment to reduce the chance people copy this bad example.
This also fixes a bug on BE architectures: tag should use cpu_to_le16, not cpu_to_le32.
----
include/uapi/linux/virtio_balloon.h +24 -8
@@ -52,15 +52,31 @@ struct virtio_balloon_config {#define VIRTIO_BALLOON_S_MEMTOT 5/* Total amount of memory */ #define VIRTIO_BALLOON_S_NR 6 +/* +* Memory statistics structure. +* Driver fills an array of these structures and passes to device. +* +* NOTE: fields are laid out in a way that would make compiler add padding +* between and after fields, so we have to use compiler-specific attributes to +* pack it, to disable this padding. This also often causes compiler to +* generate suboptimal code. +* +* We maintain this for backwards compatibility, but don't follow this example. +* +* Do something like the below instead: +* struct virtio_balloon_stat { +* __virtio16 tag; +* __u8 reserved[6]; +* __virtio64 val; +* }; +* +* In other words, add explicit reserved fields to align field and +* structure boundaries at field size, avoiding compiler padding +* without the packed attribute. +*/ struct virtio_balloon_stat { -__u16 tag; -__u64 val; +__virtio16 tag; +__virtio64 val; } __attribute__((packed)); -struct virtio_balloon_stat_modern { -__le16 tag; -__u8 reserved[6 ]; -__le64 val; -}; -#endif /* _LINUX_VIRTIO_BALLOON_H */
drivers/virtio/virtio_balloon.c +4 -15
@@ -77,10 +77,7 @@ struct virtio_balloon {/* Memory statistics */ int need_stats_update; -union { -struct virtio_balloon_stat_modern stats[VIRTIO_BALLOON_S_NR ]; -struct virtio_balloon_stat legacy_stats[VIRTIO_BALLOON_S_NR ]; - }; +struct virtio_balloon_stat stats[VIRTIO_BALLOON_S_NR ];/* To register callback in oom notifier call chain */ struct notifier_block nb;
@@ -93,10 +90,7 @@ static struct virtio_device_id id_table[] = {static void stats_sg_init (struct virtio_balloon *vb,struct scatterlist *sg) { -if (virtio_has_feature (vb->vdev,VIRTIO_F_VERSION_1 )) -sg_init_one (sg, vb->stats,sizeof (vb->stats)); -else -sg_init_one (sg, vb->legacy_stats,sizeof (vb->legacy_stats)); +sg_init_one (sg, vb->stats,sizeof (vb->stats)); }static u32 page_to_balloon_pfn (struct page *page)
@@ -225,13 +219,8 @@ static inline void update_stat(struct virtio_balloon *vb, int idx, u16 tag, u64 val) {BUG_ON (idx >=VIRTIO_BALLOON_S_NR ); -if (virtio_has_feature (vb->vdev,VIRTIO_F_VERSION_1 )) { - vb->stats[idx].tag =cpu_to_le32 (tag); - vb->stats[idx].val =cpu_to_le64 (val); - }else { - vb->legacy_stats[idx].tag = tag; - vb->legacy_stats[idx].val = val; - } + vb->stats[idx].tag =cpu_to_virtio16 (vb->vdev, tag); + vb->stats[idx].val =cpu_to_virtio64 (vb->vdev, val); }#define pages_to_bytes (x) ((u64)(x) << PAGE_SHIFT)
Signature · 1 line
MST
Michael S. Tsirkin
maintainerQuoted 28 lines · Cornelia Huck
Quoted 25 lines · On Tue, 14 Apr 2015 10:42:56 +0930 Rusty Russell <[email protected]> wrote:
Quoted 13 lines · Michael S. Tsirkin
Quoted 12 lines · Michael S. Tsirkin
Virtio 1.0 doesn't include a modern balloon device. At some point we'll likely define an incompatible interface with a different ID and different semantics. But for now, it's not a big effort to support a transitional balloon device: this has the advantage of supporting existing drivers, transparently, as well as transports that don't allow mixing virtio 0 and virtio 1 devices. And balloon is an easy device to test, so it's also useful for people to test virtio core handling of transitional devices.
The only interface issue is with the stats buffer, which has misaligned fields. We could leave it as is, but this sets a bad precedent that others might copy by mistake.
Quoted 7 lines
Quoted 4 lines
As we need to change stats code to do byteswaps for virtio 1.0, it seems easy to fix by prepending a 6 byte reserved field. I also had to change config structure field types from
__le32to__u32to match other devices. This means we need a couple of__forcetags for legacy path but that seems minor.Rusty, what are your thoughts here?
How about merging this for 4.1?I disagree with making any changes, other than allowing it to be used with
VIRTIO_F_VERSION_1.
Quoted 8 lines
Quoted 2 lines
However it doesn't seem to bother anyone else, so I've applied it for 4.1.
I'm still not really convinced about the stats change either, FWIW. Still time to reconsider?
And should we perhaps wait with merging until the spec change allowing version 1 has been accepted?
That's not how we did this historically: we require all parts (spec,qemu,linux) to be available, but don't create specific order between them. In particular, I'd strongly prefer not waiting until 4.2, that would interfere with putting virtio 1 out to use in the field.
Since both Rusty and Cornelia are against virtio_balloon_stat_modern, I accept this as the majority decision. And switching
over to __virtio tags found a bug, so I'm convinced now.
--->
virtio_balloon: drop virtio_balloon_stat_modern
Looks like we are better off sticking with the misaligned stat struct, to reduce the amount of virtio 1 specific code in balloon. So let's do it.
Add a detailed comment to reduce the chance people copy this bad example.
This also fixes a bug on BE architectures: tag should use cpu_to_le16, not cpu_to_le32.
----
include/uapi/linux/virtio_balloon.h +24 -8
@@ -52,15 +52,31 @@ struct virtio_balloon_config {#define VIRTIO_BALLOON_S_MEMTOT 5/* Total amount of memory */ #define VIRTIO_BALLOON_S_NR 6 +/* +* Memory statistics structure. +* Driver fills an array of these structures and passes to device. +* +* NOTE: fields are laid out in a way that would make compiler add padding +* between and after fields, so we have to use compiler-specific attributes to +* pack it, to disable this padding. This also often causes compiler to +* generate suboptimal code. +* +* We maintain this for backwards compatibility, but don't follow this example. +* +* Do something like the below instead: +* struct virtio_balloon_stat { +* __virtio16 tag; +* __u8 reserved[6]; +* __virtio64 val; +* }; +* +* In other words, add explicit reserved fields to align field and +* structure boundaries at field size, avoiding compiler padding +* without the packed attribute. +*/ struct virtio_balloon_stat { -__u16 tag; -__u64 val; +__virtio16 tag; +__virtio64 val; } __attribute__((packed)); -struct virtio_balloon_stat_modern { -__le16 tag; -__u8 reserved[6 ]; -__le64 val; -}; -#endif /* _LINUX_VIRTIO_BALLOON_H */
drivers/virtio/virtio_balloon.c +4 -15
@@ -77,10 +77,7 @@ struct virtio_balloon {/* Memory statistics */ int need_stats_update; -union { -struct virtio_balloon_stat_modern stats[VIRTIO_BALLOON_S_NR ]; -struct virtio_balloon_stat legacy_stats[VIRTIO_BALLOON_S_NR ]; - }; +struct virtio_balloon_stat stats[VIRTIO_BALLOON_S_NR ];/* To register callback in oom notifier call chain */ struct notifier_block nb;
@@ -93,10 +90,7 @@ static struct virtio_device_id id_table[] = {static void stats_sg_init (struct virtio_balloon *vb,struct scatterlist *sg) { -if (virtio_has_feature (vb->vdev,VIRTIO_F_VERSION_1 )) -sg_init_one (sg, vb->stats,sizeof (vb->stats)); -else -sg_init_one (sg, vb->legacy_stats,sizeof (vb->legacy_stats)); +sg_init_one (sg, vb->stats,sizeof (vb->stats)); }static u32 page_to_balloon_pfn (struct page *page)
@@ -225,13 +219,8 @@ static inline void update_stat(struct virtio_balloon *vb, int idx, u16 tag, u64 val) {BUG_ON (idx >=VIRTIO_BALLOON_S_NR ); -if (virtio_has_feature (vb->vdev,VIRTIO_F_VERSION_1 )) { - vb->stats[idx].tag =cpu_to_le32 (tag); - vb->stats[idx].val =cpu_to_le64 (val); - }else { - vb->legacy_stats[idx].tag = tag; - vb->legacy_stats[idx].val = val; - } + vb->stats[idx].tag =cpu_to_virtio16 (vb->vdev, tag); + vb->stats[idx].val =cpu_to_virtio64 (vb->vdev, val); }#define pages_to_bytes (x) ((u64)(x) << PAGE_SHIFT)
Signature · 1 line
MST
Cornelia Huck
Acked-byQuoted 8 lines · On Tue, 14 Apr 2015 11:21:11 +0200 "Michael S. Tsirkin" <[email protected]> wrote:
include/uapi/linux/virtio_balloon.h +0
-0@@ -52,15 +52,31 @@ struct virtio_balloon_config {#define VIRTIO_BALLOON_S_MEMTOT 5/* Total amount of memory */ #define VIRTIO_BALLOON_S_NR 6
include/uapi/linux/virtio_balloon.h struct virtio_balloon_config { · line 64
Quoted 10 lines
include/uapi/linux/virtio_balloon.h +10
-0+/* +* Memory statistics structure. +* Driver fills an array of these structures and passes to device. +* +* NOTE: fields are laid out in a way that would make compiler add padding +* between and after fields, so we have to use compiler-specific attributes to +* pack it, to disable this padding. This also often causes compiler to +* generate suboptimal code. +* +* We maintain this for backwards compatibility, but don't follow this example.
s/this/the existing statistics structure/
include/uapi/linux/virtio_balloon.h struct virtio_balloon_config { · line 66
Quoted 2 lines
include/uapi/linux/virtio_balloon.h +2
-0+* +* Do something like the below instead:
If you want to implement a similar structure, do...
Just that nobody gets the idea that they are supposed to implement new balloon statistics ;)
Quoted 5 lines
include/uapi/linux/virtio_balloon.h +5
-0+* struct virtio_balloon_stat { +* __virtio16 tag; +* __u8 reserved[6]; +* __virtio64 val; +* };
(...)
include/uapi/linux/virtio_balloon.h static inline void update_stat(struct virtio_balloon *vb, int idx, · line 222
Quoted 12 lines
include/uapi/linux/virtio_balloon.h +1
-7@@ -225,13 +219,8 @@ static inline void update_stat(struct virtio_balloon *vb, int idx, u16 tag, u64 val) {BUG_ON (idx >=VIRTIO_BALLOON_S_NR ); -if (virtio_has_feature (vb->vdev,VIRTIO_F_VERSION_1 )) { - vb->stats[idx].tag =cpu_to_le32 (tag); - vb->stats[idx].val =cpu_to_le64 (val); - }else { - vb->legacy_stats[idx].tag = tag; - vb->legacy_stats[idx].val = val; - } + vb->stats[idx].tag =cpu_to_virtio16 (vb->vdev, tag);
Seems that nobody seemed to care much about statistics...
include/uapi/linux/virtio_balloon.h static inline void update_stat(struct virtio_balloon *vb, int idx, · line 226
Quoted 5 lines
include/uapi/linux/virtio_balloon.h +1
-0+ vb->stats[idx].val =cpu_to_virtio64 (vb->vdev, val); }#define pages_to_bytes (x) ((u64)(x) << PAGE_SHIFT)
With these changes merged in:
Michael S. Tsirkin
maintainerQuoted 11 lines · Cornelia Huck
Quoted 8 lines · On Tue, 14 Apr 2015 11:21:11 +0200 "Michael S. Tsirkin" <[email protected]> wrote:
include/uapi/linux/virtio_balloon.h +0
-0@@ -52,15 +52,31 @@ struct virtio_balloon_config {#define VIRTIO_BALLOON_S_MEMTOT 5/* Total amount of memory */ #define VIRTIO_BALLOON_S_NR 6
Quoted 12 lines
include/uapi/linux/virtio_balloon.h struct virtio_balloon_config { · line 64
Quoted 10 lines
include/uapi/linux/virtio_balloon.h +10
-0+/* +* Memory statistics structure. +* Driver fills an array of these structures and passes to device. +* +* NOTE: fields are laid out in a way that would make compiler add padding +* between and after fields, so we have to use compiler-specific attributes to +* pack it, to disable this padding. This also often causes compiler to +* generate suboptimal code. +* +* We maintain this for backwards compatibility, but don't follow this example. s/this/the existing statistics structure/
existing seems redundant. What else? non-existing?
Quoted 16 lines
include/uapi/linux/virtio_balloon.h struct virtio_balloon_config { · line 66
Quoted 2 lines
include/uapi/linux/virtio_balloon.h +2
-0+* +* Do something like the below instead: If you want to implement a similar structure, do...
Just that nobody gets the idea that they are supposed to implement new balloon statistics ;)
Quoted 5 lines
include/uapi/linux/virtio_balloon.h +5
-0+* struct virtio_balloon_stat { +* __virtio16 tag; +* __u8 reserved[6]; +* __virtio64 val; +* }; (...)
Quoted 14 lines
include/uapi/linux/virtio_balloon.h static inline void update_stat(struct virtio_balloon *vb, int idx, · line 222
Quoted 12 lines
include/uapi/linux/virtio_balloon.h +1
-7@@ -225,13 +219,8 @@ static inline void update_stat(struct virtio_balloon *vb, int idx, u16 tag, u64 val) {BUG_ON (idx >=VIRTIO_BALLOON_S_NR ); -if (virtio_has_feature (vb->vdev,VIRTIO_F_VERSION_1 )) { - vb->stats[idx].tag =cpu_to_le32 (tag); - vb->stats[idx].val =cpu_to_le64 (val); - }else { - vb->legacy_stats[idx].tag = tag; - vb->legacy_stats[idx].val = val; - } + vb->stats[idx].tag =cpu_to_virtio16 (vb->vdev, tag);Seems that nobody seemed to care much about statistics...
Or about BE guests ;)
Quoted 9 lines
include/uapi/linux/virtio_balloon.h static inline void update_stat(struct virtio_balloon *vb, int idx, · line 226
Quoted 5 lines
include/uapi/linux/virtio_balloon.h +1
-0+ vb->stats[idx].val =cpu_to_virtio64 (vb->vdev, val); }#define pages_to_bytes (x) ((u64)(x) << PAGE_SHIFT)With these changes merged in:
Acked-by Cornelia Huck <[email protected]>
OK, here's an updated incremental patch: only comment changed.
include/uapi/linux/virtio_balloon.h +25 -8
@@ -52,15 +52,32 @@ struct virtio_balloon_config {#define VIRTIO_BALLOON_S_MEMTOT 5/* Total amount of memory */ #define VIRTIO_BALLOON_S_NR 6 +/* +* Memory statistics structure. +* Driver fills an array of these structures and passes to device. +* +* NOTE: fields are laid out in a way that would make compiler add padding +* between and after fields, so we have to use compiler-specific attributes to +* pack it, to disable this padding. This also often causes compiler to +* generate suboptimal code. +* +* We maintain this statistics structure format for backwards compatibility, +* but don't follow this example. +* +* If implementing a similar structure, do something like the below instead: +* struct virtio_balloon_stat { +* __virtio16 tag; +* __u8 reserved[6]; +* __virtio64 val; +* }; +* +* In other words, add explicit reserved fields to align field and +* structure boundaries at field size, avoiding compiler padding +* without the packed attribute. +*/ struct virtio_balloon_stat { -__u16 tag; -__u64 val; +__virtio16 tag; +__virtio64 val; } __attribute__((packed)); -struct virtio_balloon_stat_modern { -__le16 tag; -__u8 reserved[6 ]; -__le64 val; -}; -#endif /* _LINUX_VIRTIO_BALLOON_H */
drivers/virtio/virtio_balloon.c +4 -15
@@ -77,10 +77,7 @@ struct virtio_balloon {/* Memory statistics */ int need_stats_update; -union { -struct virtio_balloon_stat_modern stats[VIRTIO_BALLOON_S_NR ]; -struct virtio_balloon_stat legacy_stats[VIRTIO_BALLOON_S_NR ]; - }; +struct virtio_balloon_stat stats[VIRTIO_BALLOON_S_NR ];/* To register callback in oom notifier call chain */ struct notifier_block nb;
@@ -93,10 +90,7 @@ static struct virtio_device_id id_table[] = {static void stats_sg_init (struct virtio_balloon *vb,struct scatterlist *sg) { -if (virtio_has_feature (vb->vdev,VIRTIO_F_VERSION_1 )) -sg_init_one (sg, vb->stats,sizeof (vb->stats)); -else -sg_init_one (sg, vb->legacy_stats,sizeof (vb->legacy_stats)); +sg_init_one (sg, vb->stats,sizeof (vb->stats)); }static u32 page_to_balloon_pfn (struct page *page)
@@ -225,13 +219,8 @@ static inline void update_stat(struct virtio_balloon *vb, int idx, u16 tag, u64 val) {BUG_ON (idx >=VIRTIO_BALLOON_S_NR ); -if (virtio_has_feature (vb->vdev,VIRTIO_F_VERSION_1 )) { - vb->stats[idx].tag =cpu_to_le32 (tag); - vb->stats[idx].val =cpu_to_le64 (val); - }else { - vb->legacy_stats[idx].tag = tag; - vb->legacy_stats[idx].val = val; - } + vb->stats[idx].tag =cpu_to_virtio16 (vb->vdev, tag); + vb->stats[idx].val =cpu_to_virtio64 (vb->vdev, val); }#define pages_to_bytes (x) ((u64)(x) << PAGE_SHIFT)
Rusty Russell
Acked-by Cornelia HuckQuoted 68 lines · Michael S. Tsirkin
Quoted 11 lines · Cornelia Huck
Quoted 8 lines · On Tue, 14 Apr 2015 11:21:11 +0200 "Michael S. Tsirkin" <[email protected]> wrote:
include/uapi/linux/virtio_balloon.h +0
-0@@ -52,15 +52,31 @@ struct virtio_balloon_config {#define VIRTIO_BALLOON_S_MEMTOT 5/* Total amount of memory */ #define VIRTIO_BALLOON_S_NR 6Quoted 12 lines
include/uapi/linux/virtio_balloon.h struct virtio_balloon_config { · line 64
Quoted 10 lines
include/uapi/linux/virtio_balloon.h +10
-0+/* +* Memory statistics structure. +* Driver fills an array of these structures and passes to device. +* +* NOTE: fields are laid out in a way that would make compiler add padding +* between and after fields, so we have to use compiler-specific attributes to +* pack it, to disable this padding. This also often causes compiler to +* generate suboptimal code. +* +* We maintain this for backwards compatibility, but don't follow this example. s/this/the existing statistics structure/
existing seems redundant. What else? non-existing?
Quoted 16 lines
include/uapi/linux/virtio_balloon.h struct virtio_balloon_config { · line 66
Quoted 2 lines
include/uapi/linux/virtio_balloon.h +2
-0+* +* Do something like the below instead: If you want to implement a similar structure, do...
Just that nobody gets the idea that they are supposed to implement new balloon statistics ;)
Quoted 5 lines
include/uapi/linux/virtio_balloon.h +5
-0+* struct virtio_balloon_stat { +* __virtio16 tag; +* __u8 reserved[6]; +* __virtio64 val; +* }; (...)
Quoted 14 lines
include/uapi/linux/virtio_balloon.h static inline void update_stat(struct virtio_balloon *vb, int idx, · line 222
Quoted 12 lines
include/uapi/linux/virtio_balloon.h +1
-7@@ -225,13 +219,8 @@ static inline void update_stat(struct virtio_balloon *vb, int idx, u16 tag, u64 val) {BUG_ON (idx >=VIRTIO_BALLOON_S_NR ); -if (virtio_has_feature (vb->vdev,VIRTIO_F_VERSION_1 )) { - vb->stats[idx].tag =cpu_to_le32 (tag); - vb->stats[idx].val =cpu_to_le64 (val); - }else { - vb->legacy_stats[idx].tag = tag; - vb->legacy_stats[idx].val = val; - } + vb->stats[idx].tag =cpu_to_virtio16 (vb->vdev, tag);Seems that nobody seemed to care much about statistics...
Or about BE guests ;)
Quoted 8 lines
include/uapi/linux/virtio_balloon.h static inline void update_stat(struct virtio_balloon *vb, int idx, · line 226
Quoted 5 lines
include/uapi/linux/virtio_balloon.h +1
-0+ vb->stats[idx].val =cpu_to_virtio64 (vb->vdev, val); }#define pages_to_bytes (x) ((u64)(x) << PAGE_SHIFT)With these changes merged in:
Quoted 5 lines
Quoted 1 line
Acked-by Cornelia Huck <[email protected]>OK, here's an updated incremental patch: only comment changed.
OK, I've merged this with one change:
+static void stats_sg_init(struct virtio_balloon *vb, struct scatterlist *sg) +{
+ sg_init_one(sg, vb->stats, sizeof(vb->stats));
+}
+
...
- sg_init_one(&sg, vb->stats, sizeof(vb->stats));
+ stats_sg_init(vb, &sg);
This is no longer a meaningful change, so I removed it.
Here's the final result:
From: Michael S. Tsirkin <[email protected]>
Subject: virtio_balloon: transitional interface
Virtio 1.0 doesn't include a modern balloon device. But it's not a big change to support a transitional balloon device: this has the advantage of supporting existing drivers, transparently.
drivers/virtio/virtio_balloon.c +15 -6
@@ -214,8 +219,8 @@ static inline void update_stat(struct virtio_balloon *vb, int idx, u16 tag, u64 val) {BUG_ON (idx >=VIRTIO_BALLOON_S_NR ); - vb->stats[idx].tag = tag; - vb->stats[idx].val = val; + vb->stats[idx].tag =cpu_to_virtio16 (vb->vdev, tag); + vb->stats[idx].val =cpu_to_virtio64 (vb->vdev, val); }#define pages_to_bytes (x) ((u64)(x) << PAGE_SHIFT)
@@ -283,18 +288,27 @@ static void virtballoon_changed(struct virtio_device *vdev)static inline s64 towards_target (struct virtio_balloon *vb) { -__le32 v;s64 target; +u32 num_pages; - virtio_cread(vb->vdev, struct virtio_balloon_config, num_pages, &v); + virtio_cread(vb->vdev, struct virtio_balloon_config, num_pages, + &num_pages); - target =le32_to_cpu (v); +/* Legacy balloon config space is LE, unlike all other devices. */ +if (!virtio_has_feature (vb->vdev,VIRTIO_F_VERSION_1 )) + num_pages =le32_to_cpu ((__force __le32 )num_pages); + + target = num_pages;return target - vb->num_pages; }static void update_balloon_size (struct virtio_balloon *vb) { -__le32 actual =cpu_to_le32 (vb->num_pages); +u32 actual = vb->num_pages; + +/* Legacy balloon config space is LE, unlike all other devices. */ +if (!virtio_has_feature (vb->vdev,VIRTIO_F_VERSION_1 )) + actual = (__force u32 )cpu_to_le32 (actual); virtio_cwrite(vb->vdev, struct virtio_balloon_config, actual, &actual);
include/uapi/linux/virtio_balloon.h +28 -4
@@ -25,6 +25,7 @@* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ +#include <linux/types.h> #include <linux/virtio_ids.h> #include <linux/virtio_config.h>
@@ -38,9 +39,9 @@struct virtio_balloon_config {/* Number of pages host wants Guest to give up. */ -__le32 num_pages; +__u32 num_pages;/* Number of pages we've actually got in balloon. */ -__le32 actual; +__u32 actual; };#define VIRTIO_BALLOON_S_SWAP_IN 0/* Amount of memory swapped in */
@@ -51,9 +52,32 @@ struct virtio_balloon_config {#define VIRTIO_BALLOON_S_MEMTOT 5/* Total amount of memory */ #define VIRTIO_BALLOON_S_NR 6 +/* +* Memory statistics structure. +* Driver fills an array of these structures and passes to device. +* +* NOTE: fields are laid out in a way that would make compiler add padding +* between and after fields, so we have to use compiler-specific attributes to +* pack it, to disable this padding. This also often causes compiler to +* generate suboptimal code. +* +* We maintain this statistics structure format for backwards compatibility, +* but don't follow this example. +* +* If implementing a similar structure, do something like the below instead: +* struct virtio_balloon_stat { +* __virtio16 tag; +* __u8 reserved[6]; +* __virtio64 val; +* }; +* +* In other words, add explicit reserved fields to align field and +* structure boundaries at field size, avoiding compiler padding +* without the packed attribute. +*/ struct virtio_balloon_stat { -__u16 tag; -__u64 val; +__virtio16 tag; +__virtio64 val; } __attribute__((packed));#endif /* _LINUX_VIRTIO_BALLOON_H */
Rusty Russell
Acked-by Cornelia HuckQuoted 68 lines · Michael S. Tsirkin
Quoted 11 lines · Cornelia Huck
Quoted 8 lines · On Tue, 14 Apr 2015 11:21:11 +0200 "Michael S. Tsirkin" <[email protected]> wrote:
include/uapi/linux/virtio_balloon.h +0
-0@@ -52,15 +52,31 @@ struct virtio_balloon_config {#define VIRTIO_BALLOON_S_MEMTOT 5/* Total amount of memory */ #define VIRTIO_BALLOON_S_NR 6Quoted 12 lines
include/uapi/linux/virtio_balloon.h struct virtio_balloon_config { · line 64
Quoted 10 lines
include/uapi/linux/virtio_balloon.h +10
-0+/* +* Memory statistics structure. +* Driver fills an array of these structures and passes to device. +* +* NOTE: fields are laid out in a way that would make compiler add padding +* between and after fields, so we have to use compiler-specific attributes to +* pack it, to disable this padding. This also often causes compiler to +* generate suboptimal code. +* +* We maintain this for backwards compatibility, but don't follow this example. s/this/the existing statistics structure/
existing seems redundant. What else? non-existing?
Quoted 16 lines
include/uapi/linux/virtio_balloon.h struct virtio_balloon_config { · line 66
Quoted 2 lines
include/uapi/linux/virtio_balloon.h +2
-0+* +* Do something like the below instead: If you want to implement a similar structure, do...
Just that nobody gets the idea that they are supposed to implement new balloon statistics ;)
Quoted 5 lines
include/uapi/linux/virtio_balloon.h +5
-0+* struct virtio_balloon_stat { +* __virtio16 tag; +* __u8 reserved[6]; +* __virtio64 val; +* }; (...)
Quoted 14 lines
include/uapi/linux/virtio_balloon.h static inline void update_stat(struct virtio_balloon *vb, int idx, · line 222
Quoted 12 lines
include/uapi/linux/virtio_balloon.h +1
-7@@ -225,13 +219,8 @@ static inline void update_stat(struct virtio_balloon *vb, int idx, u16 tag, u64 val) {BUG_ON (idx >=VIRTIO_BALLOON_S_NR ); -if (virtio_has_feature (vb->vdev,VIRTIO_F_VERSION_1 )) { - vb->stats[idx].tag =cpu_to_le32 (tag); - vb->stats[idx].val =cpu_to_le64 (val); - }else { - vb->legacy_stats[idx].tag = tag; - vb->legacy_stats[idx].val = val; - } + vb->stats[idx].tag =cpu_to_virtio16 (vb->vdev, tag);Seems that nobody seemed to care much about statistics...
Or about BE guests ;)
Quoted 8 lines
include/uapi/linux/virtio_balloon.h static inline void update_stat(struct virtio_balloon *vb, int idx, · line 226
Quoted 5 lines
include/uapi/linux/virtio_balloon.h +1
-0+ vb->stats[idx].val =cpu_to_virtio64 (vb->vdev, val); }#define pages_to_bytes (x) ((u64)(x) << PAGE_SHIFT)With these changes merged in:
Quoted 5 lines
Quoted 1 line
Acked-by Cornelia Huck <[email protected]>OK, here's an updated incremental patch: only comment changed.
OK, I've merged this with one change:
+static void stats_sg_init(struct virtio_balloon *vb, struct scatterlist *sg) +{
+ sg_init_one(sg, vb->stats, sizeof(vb->stats));
+}
+
...
- sg_init_one(&sg, vb->stats, sizeof(vb->stats));
+ stats_sg_init(vb, &sg);
This is no longer a meaningful change, so I removed it.
Here's the final result:
From: Michael S. Tsirkin <[email protected]>
Subject: virtio_balloon: transitional interface
Virtio 1.0 doesn't include a modern balloon device. But it's not a big change to support a transitional balloon device: this has the advantage of supporting existing drivers, transparently.
drivers/virtio/virtio_balloon.c +15 -6
@@ -214,8 +219,8 @@ static inline void update_stat(struct virtio_balloon *vb, int idx, u16 tag, u64 val) {BUG_ON (idx >=VIRTIO_BALLOON_S_NR ); - vb->stats[idx].tag = tag; - vb->stats[idx].val = val; + vb->stats[idx].tag =cpu_to_virtio16 (vb->vdev, tag); + vb->stats[idx].val =cpu_to_virtio64 (vb->vdev, val); }#define pages_to_bytes (x) ((u64)(x) << PAGE_SHIFT)
@@ -283,18 +288,27 @@ static void virtballoon_changed(struct virtio_device *vdev)static inline s64 towards_target (struct virtio_balloon *vb) { -__le32 v;s64 target; +u32 num_pages; - virtio_cread(vb->vdev, struct virtio_balloon_config, num_pages, &v); + virtio_cread(vb->vdev, struct virtio_balloon_config, num_pages, + &num_pages); - target =le32_to_cpu (v); +/* Legacy balloon config space is LE, unlike all other devices. */ +if (!virtio_has_feature (vb->vdev,VIRTIO_F_VERSION_1 )) + num_pages =le32_to_cpu ((__force __le32 )num_pages); + + target = num_pages;return target - vb->num_pages; }static void update_balloon_size (struct virtio_balloon *vb) { -__le32 actual =cpu_to_le32 (vb->num_pages); +u32 actual = vb->num_pages; + +/* Legacy balloon config space is LE, unlike all other devices. */ +if (!virtio_has_feature (vb->vdev,VIRTIO_F_VERSION_1 )) + actual = (__force u32 )cpu_to_le32 (actual); virtio_cwrite(vb->vdev, struct virtio_balloon_config, actual, &actual);
include/uapi/linux/virtio_balloon.h +28 -4
@@ -25,6 +25,7 @@* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ +#include <linux/types.h> #include <linux/virtio_ids.h> #include <linux/virtio_config.h>
@@ -38,9 +39,9 @@struct virtio_balloon_config {/* Number of pages host wants Guest to give up. */ -__le32 num_pages; +__u32 num_pages;/* Number of pages we've actually got in balloon. */ -__le32 actual; +__u32 actual; };#define VIRTIO_BALLOON_S_SWAP_IN 0/* Amount of memory swapped in */
@@ -51,9 +52,32 @@ struct virtio_balloon_config {#define VIRTIO_BALLOON_S_MEMTOT 5/* Total amount of memory */ #define VIRTIO_BALLOON_S_NR 6 +/* +* Memory statistics structure. +* Driver fills an array of these structures and passes to device. +* +* NOTE: fields are laid out in a way that would make compiler add padding +* between and after fields, so we have to use compiler-specific attributes to +* pack it, to disable this padding. This also often causes compiler to +* generate suboptimal code. +* +* We maintain this statistics structure format for backwards compatibility, +* but don't follow this example. +* +* If implementing a similar structure, do something like the below instead: +* struct virtio_balloon_stat { +* __virtio16 tag; +* __u8 reserved[6]; +* __virtio64 val; +* }; +* +* In other words, add explicit reserved fields to align field and +* structure boundaries at field size, avoiding compiler padding +* without the packed attribute. +*/ struct virtio_balloon_stat { -__u16 tag; -__u64 val; +__virtio16 tag; +__virtio64 val; } __attribute__((packed));#endif /* _LINUX_VIRTIO_BALLOON_H */
Quoted 18 lines · On Wed, 15 Apr 2015 10:15:20 +0930 Rusty Russell <[email protected]> wrote:
OK, I've merged this with one change:
(quoted diff) +6
-1+static void stats_sg_init(struct virtio_balloon *vb, struct scatterlist *sg) +{ + sg_init_one(sg, vb->stats, sizeof(vb->stats)); +} + - sg_init_one(&sg, vb->stats, sizeof(vb->stats)); + stats_sg_init(vb, &sg);This is no longer a meaningful change, so I removed it.
Here's the final result:
From: Michael S. Tsirkin <[email protected]>
Subject:virtio_balloon: transitional interface
Quoted 9 lines
Virtio 1.0 doesn't include a modern balloon device. But it's not a big change to support a transitional balloon device: this has the advantage of supporting existing drivers, transparently.
Signed-off-by Michael S. Tsirkin <[email protected]>Acked-by Cornelia Huck <[email protected]>Signed-off-by Rusty Russell <[email protected]>
Looks good to me.
Quoted 18 lines · On Wed, 15 Apr 2015 10:15:20 +0930 Rusty Russell <[email protected]> wrote:
OK, I've merged this with one change:
(quoted diff) +6
-1+static void stats_sg_init(struct virtio_balloon *vb, struct scatterlist *sg) +{ + sg_init_one(sg, vb->stats, sizeof(vb->stats)); +} + - sg_init_one(&sg, vb->stats, sizeof(vb->stats)); + stats_sg_init(vb, &sg);This is no longer a meaningful change, so I removed it.
Here's the final result:
From: Michael S. Tsirkin <[email protected]>
Subject:virtio_balloon: transitional interface
Quoted 9 lines
Virtio 1.0 doesn't include a modern balloon device. But it's not a big change to support a transitional balloon device: this has the advantage of supporting existing drivers, transparently.
Signed-off-by Michael S. Tsirkin <[email protected]>Acked-by Cornelia Huck <[email protected]>Signed-off-by Rusty Russell <[email protected]>
Looks good to me.
Michael S. Tsirkin
maintainerQuoted 93 lines · Rusty Russell
Quoted 68 lines · Michael S. Tsirkin
Quoted 11 lines · Cornelia Huck
Quoted 8 lines · On Tue, 14 Apr 2015 11:21:11 +0200 "Michael S. Tsirkin" <[email protected]> wrote:
include/uapi/linux/virtio_balloon.h +0
-0@@ -52,15 +52,31 @@ struct virtio_balloon_config {#define VIRTIO_BALLOON_S_MEMTOT 5/* Total amount of memory */ #define VIRTIO_BALLOON_S_NR 6Quoted 12 lines
include/uapi/linux/virtio_balloon.h struct virtio_balloon_config {
Quoted 10 lines
include/uapi/linux/virtio_balloon.h +10
-0+/* +* Memory statistics structure. +* Driver fills an array of these structures and passes to device. +* +* NOTE: fields are laid out in a way that would make compiler add padding +* between and after fields, so we have to use compiler-specific attributes to +* pack it, to disable this padding. This also often causes compiler to +* generate suboptimal code. +* +* We maintain this for backwards compatibility, but don't follow this example. s/this/the existing statistics structure/
existing seems redundant. What else? non-existing?
Quoted 16 lines
include/uapi/linux/virtio_balloon.h struct virtio_balloon_config { · line 66
Quoted 2 lines
include/uapi/linux/virtio_balloon.h +2
-0+* +* Do something like the below instead: If you want to implement a similar structure, do...
Just that nobody gets the idea that they are supposed to implement new balloon statistics ;)
Quoted 5 lines
include/uapi/linux/virtio_balloon.h +5
-0+* struct virtio_balloon_stat { +* __virtio16 tag; +* __u8 reserved[6]; +* __virtio64 val; +* }; (...)
Quoted 14 lines
include/uapi/linux/virtio_balloon.h static inline void update_stat(struct virtio_balloon *vb, int idx, · line 222
Quoted 12 lines
include/uapi/linux/virtio_balloon.h +1
-7@@ -225,13 +219,8 @@ static inline void update_stat(struct virtio_balloon *vb, int idx, u16 tag, u64 val) {BUG_ON (idx >=VIRTIO_BALLOON_S_NR ); -if (virtio_has_feature (vb->vdev,VIRTIO_F_VERSION_1 )) { - vb->stats[idx].tag =cpu_to_le32 (tag); - vb->stats[idx].val =cpu_to_le64 (val); - }else { - vb->legacy_stats[idx].tag = tag; - vb->legacy_stats[idx].val = val; - } + vb->stats[idx].tag =cpu_to_virtio16 (vb->vdev, tag);Seems that nobody seemed to care much about statistics...
Or about BE guests ;)
Quoted 8 lines
include/uapi/linux/virtio_balloon.h static inline void update_stat(struct virtio_balloon *vb, int idx, · line 226
Quoted 5 lines
include/uapi/linux/virtio_balloon.h +1
-0+ vb->stats[idx].val =cpu_to_virtio64 (vb->vdev, val); }#define pages_to_bytes (x) ((u64)(x) << PAGE_SHIFT)With these changes merged in:
Quoted 5 lines
Quoted 1 line
Acked-by Cornelia Huck <[email protected]>OK, here's an updated incremental patch: only comment changed.
OK, I've merged this with one change:
(quoted diff) +6
-1+static void stats_sg_init(struct virtio_balloon *vb, struct scatterlist *sg) +{ + sg_init_one(sg, vb->stats, sizeof(vb->stats)); +} + - sg_init_one(&sg, vb->stats, sizeof(vb->stats)); + stats_sg_init(vb, &sg);This is no longer a meaningful change, so I removed it.
Here's the final result:
From: Michael S. Tsirkin <[email protected]>
Subject:virtio_balloon: transitional interface
Quoted 8 lines
Virtio 1.0 doesn't include a modern balloon device. But it's not a big change to support a transitional balloon device: this has the advantage of supporting existing drivers, transparently.
Signed-off-by Michael S. Tsirkin <[email protected]>Acked-by Cornelia Huck <[email protected]>Signed-off-by Rusty Russell <[email protected]>
Fine by me.
Quoted 106 lines
drivers/virtio/virtio_balloon.c +15
-6@@ -214,8 +219,8 @@ static inline void update_stat(struct virtio_balloon *vb, int idx, u16 tag, u64 val) {BUG_ON (idx >=VIRTIO_BALLOON_S_NR ); - vb->stats[idx].tag = tag; - vb->stats[idx].val = val; + vb->stats[idx].tag =cpu_to_virtio16 (vb->vdev, tag); + vb->stats[idx].val =cpu_to_virtio64 (vb->vdev, val); }#define pages_to_bytes (x) ((u64)(x) << PAGE_SHIFT)@@ -283,18 +288,27 @@ static void virtballoon_changed(struct virtio_device *vdev)static inline s64 towards_target (struct virtio_balloon *vb) { -__le32 v;s64 target; +u32 num_pages; - virtio_cread(vb->vdev, struct virtio_balloon_config, num_pages, &v); + virtio_cread(vb->vdev, struct virtio_balloon_config, num_pages, + &num_pages); - target =le32_to_cpu (v); +/* Legacy balloon config space is LE, unlike all other devices. */ +if (!virtio_has_feature (vb->vdev,VIRTIO_F_VERSION_1 )) + num_pages =le32_to_cpu ((__force __le32 )num_pages); + + target = num_pages;return target - vb->num_pages; }static void update_balloon_size (struct virtio_balloon *vb) { -__le32 actual =cpu_to_le32 (vb->num_pages); +u32 actual = vb->num_pages; + +/* Legacy balloon config space is LE, unlike all other devices. */ +if (!virtio_has_feature (vb->vdev,VIRTIO_F_VERSION_1 )) + actual = (__force u32 )cpu_to_le32 (actual); virtio_cwrite(vb->vdev, struct virtio_balloon_config, actual, &actual);include/uapi/linux/virtio_balloon.h +28
-4@@ -25,6 +25,7 @@* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ +#include <linux/types.h> #include <linux/virtio_ids.h> #include <linux/virtio_config.h> @@ -38,9 +39,9 @@struct virtio_balloon_config {/* Number of pages host wants Guest to give up. */ -__le32 num_pages; +__u32 num_pages;/* Number of pages we've actually got in balloon. */ -__le32 actual; +__u32 actual; };#define VIRTIO_BALLOON_S_SWAP_IN 0/* Amount of memory swapped in */ @@ -51,9 +52,32 @@ struct virtio_balloon_config {#define VIRTIO_BALLOON_S_MEMTOT 5/* Total amount of memory */ #define VIRTIO_BALLOON_S_NR 6 +/* +* Memory statistics structure. +* Driver fills an array of these structures and passes to device. +* +* NOTE: fields are laid out in a way that would make compiler add padding +* between and after fields, so we have to use compiler-specific attributes to +* pack it, to disable this padding. This also often causes compiler to +* generate suboptimal code. +* +* We maintain this statistics structure format for backwards compatibility, +* but don't follow this example. +* +* If implementing a similar structure, do something like the below instead: +* struct virtio_balloon_stat { +* __virtio16 tag; +* __u8 reserved[6]; +* __virtio64 val; +* }; +* +* In other words, add explicit reserved fields to align field and +* structure boundaries at field size, avoiding compiler padding +* without the packed attribute. +*/ struct virtio_balloon_stat { -__u16 tag; -__u64 val; +__virtio16 tag; +__virtio64 val; } __attribute__((packed));#endif /* _LINUX_VIRTIO_BALLOON_H */
Michael S. Tsirkin
maintainerQuoted 93 lines · Rusty Russell
Quoted 68 lines · Michael S. Tsirkin
Quoted 11 lines · Cornelia Huck
Quoted 8 lines · On Tue, 14 Apr 2015 11:21:11 +0200 "Michael S. Tsirkin" <[email protected]> wrote:
include/uapi/linux/virtio_balloon.h +0
-0@@ -52,15 +52,31 @@ struct virtio_balloon_config {#define VIRTIO_BALLOON_S_MEMTOT 5/* Total amount of memory */ #define VIRTIO_BALLOON_S_NR 6Quoted 12 lines
include/uapi/linux/virtio_balloon.h struct virtio_balloon_config {
Quoted 10 lines
include/uapi/linux/virtio_balloon.h +10
-0+/* +* Memory statistics structure. +* Driver fills an array of these structures and passes to device. +* +* NOTE: fields are laid out in a way that would make compiler add padding +* between and after fields, so we have to use compiler-specific attributes to +* pack it, to disable this padding. This also often causes compiler to +* generate suboptimal code. +* +* We maintain this for backwards compatibility, but don't follow this example. s/this/the existing statistics structure/
existing seems redundant. What else? non-existing?
Quoted 16 lines
include/uapi/linux/virtio_balloon.h struct virtio_balloon_config { · line 66
Quoted 2 lines
include/uapi/linux/virtio_balloon.h +2
-0+* +* Do something like the below instead: If you want to implement a similar structure, do...
Just that nobody gets the idea that they are supposed to implement new balloon statistics ;)
Quoted 5 lines
include/uapi/linux/virtio_balloon.h +5
-0+* struct virtio_balloon_stat { +* __virtio16 tag; +* __u8 reserved[6]; +* __virtio64 val; +* }; (...)
Quoted 14 lines
include/uapi/linux/virtio_balloon.h static inline void update_stat(struct virtio_balloon *vb, int idx, · line 222
Quoted 12 lines
include/uapi/linux/virtio_balloon.h +1
-7@@ -225,13 +219,8 @@ static inline void update_stat(struct virtio_balloon *vb, int idx, u16 tag, u64 val) {BUG_ON (idx >=VIRTIO_BALLOON_S_NR ); -if (virtio_has_feature (vb->vdev,VIRTIO_F_VERSION_1 )) { - vb->stats[idx].tag =cpu_to_le32 (tag); - vb->stats[idx].val =cpu_to_le64 (val); - }else { - vb->legacy_stats[idx].tag = tag; - vb->legacy_stats[idx].val = val; - } + vb->stats[idx].tag =cpu_to_virtio16 (vb->vdev, tag);Seems that nobody seemed to care much about statistics...
Or about BE guests ;)
Quoted 8 lines
include/uapi/linux/virtio_balloon.h static inline void update_stat(struct virtio_balloon *vb, int idx, · line 226
Quoted 5 lines
include/uapi/linux/virtio_balloon.h +1
-0+ vb->stats[idx].val =cpu_to_virtio64 (vb->vdev, val); }#define pages_to_bytes (x) ((u64)(x) << PAGE_SHIFT)With these changes merged in:
Quoted 5 lines
Quoted 1 line
Acked-by Cornelia Huck <[email protected]>OK, here's an updated incremental patch: only comment changed.
OK, I've merged this with one change:
(quoted diff) +6
-1+static void stats_sg_init(struct virtio_balloon *vb, struct scatterlist *sg) +{ + sg_init_one(sg, vb->stats, sizeof(vb->stats)); +} + - sg_init_one(&sg, vb->stats, sizeof(vb->stats)); + stats_sg_init(vb, &sg);This is no longer a meaningful change, so I removed it.
Here's the final result:
From: Michael S. Tsirkin <[email protected]>
Subject:virtio_balloon: transitional interface
Quoted 8 lines
Virtio 1.0 doesn't include a modern balloon device. But it's not a big change to support a transitional balloon device: this has the advantage of supporting existing drivers, transparently.
Signed-off-by Michael S. Tsirkin <[email protected]>Acked-by Cornelia Huck <[email protected]>Signed-off-by Rusty Russell <[email protected]>
Fine by me.
Quoted 106 lines
drivers/virtio/virtio_balloon.c +15
-6@@ -214,8 +219,8 @@ static inline void update_stat(struct virtio_balloon *vb, int idx, u16 tag, u64 val) {BUG_ON (idx >=VIRTIO_BALLOON_S_NR ); - vb->stats[idx].tag = tag; - vb->stats[idx].val = val; + vb->stats[idx].tag =cpu_to_virtio16 (vb->vdev, tag); + vb->stats[idx].val =cpu_to_virtio64 (vb->vdev, val); }#define pages_to_bytes (x) ((u64)(x) << PAGE_SHIFT)@@ -283,18 +288,27 @@ static void virtballoon_changed(struct virtio_device *vdev)static inline s64 towards_target (struct virtio_balloon *vb) { -__le32 v;s64 target; +u32 num_pages; - virtio_cread(vb->vdev, struct virtio_balloon_config, num_pages, &v); + virtio_cread(vb->vdev, struct virtio_balloon_config, num_pages, + &num_pages); - target =le32_to_cpu (v); +/* Legacy balloon config space is LE, unlike all other devices. */ +if (!virtio_has_feature (vb->vdev,VIRTIO_F_VERSION_1 )) + num_pages =le32_to_cpu ((__force __le32 )num_pages); + + target = num_pages;return target - vb->num_pages; }static void update_balloon_size (struct virtio_balloon *vb) { -__le32 actual =cpu_to_le32 (vb->num_pages); +u32 actual = vb->num_pages; + +/* Legacy balloon config space is LE, unlike all other devices. */ +if (!virtio_has_feature (vb->vdev,VIRTIO_F_VERSION_1 )) + actual = (__force u32 )cpu_to_le32 (actual); virtio_cwrite(vb->vdev, struct virtio_balloon_config, actual, &actual);include/uapi/linux/virtio_balloon.h +28
-4@@ -25,6 +25,7 @@* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ +#include <linux/types.h> #include <linux/virtio_ids.h> #include <linux/virtio_config.h> @@ -38,9 +39,9 @@struct virtio_balloon_config {/* Number of pages host wants Guest to give up. */ -__le32 num_pages; +__u32 num_pages;/* Number of pages we've actually got in balloon. */ -__le32 actual; +__u32 actual; };#define VIRTIO_BALLOON_S_SWAP_IN 0/* Amount of memory swapped in */ @@ -51,9 +52,32 @@ struct virtio_balloon_config {#define VIRTIO_BALLOON_S_MEMTOT 5/* Total amount of memory */ #define VIRTIO_BALLOON_S_NR 6 +/* +* Memory statistics structure. +* Driver fills an array of these structures and passes to device. +* +* NOTE: fields are laid out in a way that would make compiler add padding +* between and after fields, so we have to use compiler-specific attributes to +* pack it, to disable this padding. This also often causes compiler to +* generate suboptimal code. +* +* We maintain this statistics structure format for backwards compatibility, +* but don't follow this example. +* +* If implementing a similar structure, do something like the below instead: +* struct virtio_balloon_stat { +* __virtio16 tag; +* __u8 reserved[6]; +* __virtio64 val; +* }; +* +* In other words, add explicit reserved fields to align field and +* structure boundaries at field size, avoiding compiler padding +* without the packed attribute. +*/ struct virtio_balloon_stat { -__u16 tag; -__u64 val; +__virtio16 tag; +__virtio64 val; } __attribute__((packed));#endif /* _LINUX_VIRTIO_BALLOON_H */