diff mbox series

[kvmtool,4/4] virtio/blk: Avoid taking pointer to packed struct

Message ID 20190503171544.260901-5-andre.przywara@arm.com (mailing list archive)
State New, archived
Headers show
Series kvmtool: clang/GCC9 fixes | expand

Commit Message

Andre Przywara May 3, 2019, 5:15 p.m. UTC
clang and GCC9 refuse to compile virtio/blk.c with the following message:
virtio/blk.c:161:37: error: taking address of packed member 'geometry' of class
      or structure 'virtio_blk_config' may result in an unaligned pointer value
      [-Werror,-Waddress-of-packed-member]
        struct virtio_blk_geometry *geo = &conf->geometry;

Since struct virtio_blk_geometry is in a kernel header, we can't do much
about the packed attribute, but as Peter pointed out, the solution is
rather simple: just get rid of the convenience variable and use the
original struct member directly.

Suggested-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Andre Przywara <andre.przywara@arm.com>
---
 virtio/blk.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Jean-Philippe Brucker May 29, 2019, 2:50 p.m. UTC | #1
On 03/05/2019 18:15, Andre Przywara wrote:
> clang and GCC9 refuse to compile virtio/blk.c with the following message:
> virtio/blk.c:161:37: error: taking address of packed member 'geometry' of class
>       or structure 'virtio_blk_config' may result in an unaligned pointer value
>       [-Werror,-Waddress-of-packed-member]
>         struct virtio_blk_geometry *geo = &conf->geometry;
> 
> Since struct virtio_blk_geometry is in a kernel header, we can't do much
> about the packed attribute, but as Peter pointed out, the solution is
> rather simple: just get rid of the convenience variable and use the
> original struct member directly.
> 
> Suggested-by: Peter Maydell <peter.maydell@linaro.org>
> Signed-off-by: Andre Przywara <andre.przywara@arm.com>

Reviewed-by: Jean-Philippe Brucker <jean-philippe.brucker@arm.com>

I just encountered this one with GCC 9.1

> ---
>  virtio/blk.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/virtio/blk.c b/virtio/blk.c
> index 50db6f5f..f267be15 100644
> --- a/virtio/blk.c
> +++ b/virtio/blk.c
> @@ -161,7 +161,6 @@ static void set_guest_features(struct kvm *kvm, void *dev, u32 features)
>  {
>  	struct blk_dev *bdev = dev;
>  	struct virtio_blk_config *conf = &bdev->blk_config;
> -	struct virtio_blk_geometry *geo = &conf->geometry;
>  
>  	bdev->features = features;
>  
> @@ -170,7 +169,8 @@ static void set_guest_features(struct kvm *kvm, void *dev, u32 features)
>  	conf->seg_max = virtio_host_to_guest_u32(&bdev->vdev, conf->seg_max);
>  
>  	/* Geometry */
> -	geo->cylinders = virtio_host_to_guest_u16(&bdev->vdev, geo->cylinders);
> +	conf->geometry.cylinders = virtio_host_to_guest_u16(&bdev->vdev,
> +						conf->geometry.cylinders);
>  
>  	conf->blk_size = virtio_host_to_guest_u32(&bdev->vdev, conf->blk_size);
>  	conf->min_io_size = virtio_host_to_guest_u16(&bdev->vdev, conf->min_io_size);
>
diff mbox series

Patch

diff --git a/virtio/blk.c b/virtio/blk.c
index 50db6f5f..f267be15 100644
--- a/virtio/blk.c
+++ b/virtio/blk.c
@@ -161,7 +161,6 @@  static void set_guest_features(struct kvm *kvm, void *dev, u32 features)
 {
 	struct blk_dev *bdev = dev;
 	struct virtio_blk_config *conf = &bdev->blk_config;
-	struct virtio_blk_geometry *geo = &conf->geometry;
 
 	bdev->features = features;
 
@@ -170,7 +169,8 @@  static void set_guest_features(struct kvm *kvm, void *dev, u32 features)
 	conf->seg_max = virtio_host_to_guest_u32(&bdev->vdev, conf->seg_max);
 
 	/* Geometry */
-	geo->cylinders = virtio_host_to_guest_u16(&bdev->vdev, geo->cylinders);
+	conf->geometry.cylinders = virtio_host_to_guest_u16(&bdev->vdev,
+						conf->geometry.cylinders);
 
 	conf->blk_size = virtio_host_to_guest_u32(&bdev->vdev, conf->blk_size);
 	conf->min_io_size = virtio_host_to_guest_u16(&bdev->vdev, conf->min_io_size);