virtualization.lists.linux-foundation.org archive mirror
 help / color / mirror / Atom feed
From: "Michael S. Tsirkin" <mst@redhat.com>
To: Cornelia Huck <cornelia.huck@de.ibm.com>
Cc: thuth@linux.vnet.ibm.com, qemu-devel@nongnu.org,
	kvm@vger.kernel.org, virtualization@lists.linux-foundation.org
Subject: Re: [Qemu-devel] [PATCH RFC 03/11] virtio: endianess conversion helpers
Date: Wed, 22 Oct 2014 12:04:32 +0300	[thread overview]
Message-ID: <20141022090432.GA9258@redhat.com> (raw)
In-Reply-To: <1412692792-12325-4-git-send-email-cornelia.huck@de.ibm.com>

On Tue, Oct 07, 2014 at 04:39:44PM +0200, Cornelia Huck wrote:
> Provide helper functions that convert from/to LE for virtio devices that
> are not operating in legacy mode. We check for the VERSION_1 feature bit
> to determine that.
> 
> Based on original patches by Rusty Russell and Thomas Huth.
> 
> Reviewed-by: David Hildenbrand <dahi@linux.vnet.ibm.com>
> Signed-off-by: Cornelia Huck <cornelia.huck@de.ibm.com>


I'm worried that this might miss some conversion.
Let's add new typedefs __virtio16/__virtio32/__virtio64
instead.

This way we can use static checkers to catch bugs.

This is what my patch does, let me try to split
it up so parts are reusable for you.


Also if we do this, then virtio32_to_cpu is a better API
since it's closer to the type name.

> ---
>  drivers/virtio/virtio.c            |    4 ++++
>  include/linux/virtio.h             |   40 ++++++++++++++++++++++++++++++++++++
>  include/uapi/linux/virtio_config.h |    3 +++
>  3 files changed, 47 insertions(+)
> 
> diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
> index cfd5d00..8f74cd6 100644
> --- a/drivers/virtio/virtio.c
> +++ b/drivers/virtio/virtio.c
> @@ -144,6 +144,10 @@ static int virtio_dev_probe(struct device *_d)
>  		if (device_features & (1ULL << i))
>  			dev->features |= (1ULL << i);
>  
> +	/* Version 1.0 compliant devices set the VIRTIO_F_VERSION_1 bit */
> +	if (device_features & (1ULL << VIRTIO_F_VERSION_1))
> +		dev->features |= (1ULL << VIRTIO_F_VERSION_1);
> +
>  	dev->config->finalize_features(dev);
>  
>  	err = drv->probe(dev);
> diff --git a/include/linux/virtio.h b/include/linux/virtio.h
> index a24b41f..68cadd4 100644
> --- a/include/linux/virtio.h
> +++ b/include/linux/virtio.h
> @@ -9,6 +9,7 @@
>  #include <linux/mod_devicetable.h>
>  #include <linux/gfp.h>
>  #include <linux/vringh.h>
> +#include <uapi/linux/virtio_config.h>
>  
>  /**
>   * virtqueue - a queue to register buffers for sending or receiving.
> @@ -102,6 +103,11 @@ static inline struct virtio_device *dev_to_virtio(struct device *_dev)
>  	return container_of(_dev, struct virtio_device, dev);
>  }
>  
> +static inline bool virtio_device_legacy(const struct virtio_device *dev)
> +{
> +	return !(dev->features & (1ULL << VIRTIO_F_VERSION_1));
> +}
> +
>  int register_virtio_device(struct virtio_device *dev);
>  void unregister_virtio_device(struct virtio_device *dev);
>  
> @@ -149,4 +155,38 @@ void unregister_virtio_driver(struct virtio_driver *drv);
>  #define module_virtio_driver(__virtio_driver) \
>  	module_driver(__virtio_driver, register_virtio_driver, \
>  			unregister_virtio_driver)
> +
> +/*
> + * v1.0 specifies LE headers, legacy was native endian. Therefore, we must
> + * convert from/to LE if and only if vdev is not legacy.
> + */
> +static inline u16 virtio_to_cpu_u16(const struct virtio_device *vdev, u16 v)
> +{
> +	return virtio_device_legacy(vdev) ? v : le16_to_cpu(v);
> +}
> +
> +static inline u32 virtio_to_cpu_u32(const struct virtio_device *vdev, u32 v)
> +{
> +	return virtio_device_legacy(vdev) ? v : le32_to_cpu(v);
> +}
> +
> +static inline u64 virtio_to_cpu_u64(const struct virtio_device *vdev, u64 v)
> +{
> +	return virtio_device_legacy(vdev) ? v : le64_to_cpu(v);
> +}
> +
> +static inline u16 cpu_to_virtio_u16(const struct virtio_device *vdev, u16 v)
> +{
> +	return virtio_device_legacy(vdev) ? v : cpu_to_le16(v);
> +}
> +
> +static inline u32 cpu_to_virtio_u32(const struct virtio_device *vdev, u32 v)
> +{
> +	return virtio_device_legacy(vdev) ? v : cpu_to_le32(v);
> +}
> +
> +static inline u64 cpu_to_virtio_u64(const struct virtio_device *vdev, u64 v)
> +{
> +	return virtio_device_legacy(vdev) ? v : cpu_to_le64(v);
> +}
>  #endif /* _LINUX_VIRTIO_H */

Would be nicer to allow callers to pass in the legacy flag I think?
This way they can keep it on stack to avoid re-reading features
all the time ...


> diff --git a/include/uapi/linux/virtio_config.h b/include/uapi/linux/virtio_config.h
> index 3ce768c..80e7381 100644
> --- a/include/uapi/linux/virtio_config.h
> +++ b/include/uapi/linux/virtio_config.h
> @@ -54,4 +54,7 @@
>  /* Can the device handle any descriptor layout? */
>  #define VIRTIO_F_ANY_LAYOUT		27
>  
> +/* v1.0 compliant. */
> +#define VIRTIO_F_VERSION_1		32
> +
>  #endif /* _UAPI_LINUX_VIRTIO_CONFIG_H */
> -- 
> 1.7.9.5
> 

  parent reply	other threads:[~2014-10-22  9:04 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <1412692792-12325-1-git-send-email-cornelia.huck@de.ibm.com>
2014-10-07 14:39 ` [PATCH RFC 01/11] virtio: use u32, not bitmap for struct virtio_device's features Cornelia Huck
2014-10-07 14:39 ` [PATCH RFC 02/11] virtio: add support for 64 bit features Cornelia Huck
2014-10-07 14:39 ` [PATCH RFC 03/11] virtio: endianess conversion helpers Cornelia Huck
2014-10-07 14:39 ` [PATCH RFC 04/11] virtio_ring: implement endian reversal based on VERSION_1 feature Cornelia Huck
2014-10-07 14:39 ` [PATCH RFC 05/11] virtio_config: endian conversion for v1.0 Cornelia Huck
2014-10-07 14:39 ` [PATCH RFC 06/11] virtio: allow transports to get avail/used addresses Cornelia Huck
2014-10-07 14:39 ` [PATCH RFC 07/11] virtio_net: use v1.0 endian Cornelia Huck
2014-10-07 14:39 ` [PATCH RFC 08/11] virtio_blk: use virtio " Cornelia Huck
2014-10-07 14:39 ` [PATCH RFC 09/11] KVM: s390: Set virtio-ccw transport revision Cornelia Huck
2014-10-07 14:39 ` [PATCH RFC 10/11] KVM: s390: virtio-ccw revision 1 SET_VQ Cornelia Huck
2014-10-07 14:39 ` [PATCH RFC 11/11] KVM: s390: enable virtio-ccw revision 1 Cornelia Huck
     [not found] ` <1412692792-12325-9-git-send-email-cornelia.huck@de.ibm.com>
2014-10-13  5:58   ` [PATCH RFC 08/11] virtio_blk: use virtio v1.0 endian Rusty Russell
     [not found]   ` <878ukkecjb.fsf@rustcorp.com.au>
2014-10-13 10:42     ` Cornelia Huck
     [not found] ` <1412692792-12325-4-git-send-email-cornelia.huck@de.ibm.com>
2014-10-22  9:04   ` Michael S. Tsirkin [this message]
     [not found] ` <1412692792-12325-5-git-send-email-cornelia.huck@de.ibm.com>
2014-10-22 14:02   ` [PATCH RFC 04/11] virtio_ring: implement endian reversal based on VERSION_1 feature Michael S. Tsirkin
2014-10-22 14:28     ` Cornelia Huck
2014-10-22 14:37       ` Michael S. Tsirkin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20141022090432.GA9258@redhat.com \
    --to=mst@redhat.com \
    --cc=cornelia.huck@de.ibm.com \
    --cc=kvm@vger.kernel.org \
    --cc=qemu-devel@nongnu.org \
    --cc=thuth@linux.vnet.ibm.com \
    --cc=virtualization@lists.linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).