All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Michael S. Tsirkin" <mst@redhat.com>
To: "Cédric Le Goater" <clg@fr.ibm.com>
Cc: kvm@vger.kernel.org, kvm-ppc@vger.kernel.org, agraf@suse.de,
	paulus@samba.org, gkurz@linux.vnet.ibm.com.patch, aik@ozlabs.ru
Subject: Re: [RFC PATCH 4/4] vhost_net: byteswap virtio_net header
Date: Mon, 03 Nov 2014 16:25:25 +0000	[thread overview]
Message-ID: <20141103162525.GC24877@redhat.com> (raw)
In-Reply-To: <1414571925-16918-5-git-send-email-clg@fr.ibm.com>

On Wed, Oct 29, 2014 at 09:38:45AM +0100, Cédric Le Goater wrote:
> Signed-off-by: Cédric Le Goater <clg@fr.ibm.com>

This patch casts userspace pointers to void *,
this is generally unsafe. In particular sparse will warn.

> ---
>  drivers/vhost/net.c |   39 ++++++++++++++++++++++++++++++---------
>  1 file changed, 30 insertions(+), 9 deletions(-)
> 
> diff --git a/drivers/vhost/net.c b/drivers/vhost/net.c
> index 8dae2f724a35..f2d5f585dae9 100644
> --- a/drivers/vhost/net.c
> +++ b/drivers/vhost/net.c
> @@ -18,6 +18,7 @@
>  #include <linux/file.h>
>  #include <linux/slab.h>
>  #include <linux/vmalloc.h>
> +#include <linux/swab.h>
>  
>  #include <linux/net.h>
>  #include <linux/if_packet.h>
> @@ -329,6 +330,14 @@ static void vhost_zerocopy_callback(struct ubuf_info *ubuf, bool success)
>  	rcu_read_unlock_bh();
>  }
>  
> +static void virtio_net_hdr_swap(struct virtio_net_hdr *hdr)
> +{
> +	hdr->hdr_len     = swab16(hdr->hdr_len);
> +	hdr->gso_size    = swab16(hdr->gso_size);
> +	hdr->csum_start  = swab16(hdr->csum_start);
> +	hdr->csum_offset = swab16(hdr->csum_offset);
> +}
> +
>  /* Expects to be always run from workqueue - which acts as
>   * read-size critical section for our kind of RCU. */
>  static void handle_tx(struct vhost_net *net)
> @@ -346,7 +355,7 @@ static void handle_tx(struct vhost_net *net)
>  		.msg_flags = MSG_DONTWAIT,
>  	};
>  	size_t len, total_len = 0;
> -	int err;
> +	int err, has_vnet_hdr;
>  	size_t hdr_size;
>  	struct socket *sock;
>  	struct vhost_net_ubuf_ref *uninitialized_var(ubufs);
> @@ -359,6 +368,7 @@ static void handle_tx(struct vhost_net *net)
>  
>  	vhost_disable_notify(&net->dev, vq);
>  
> +	has_vnet_hdr = vhost_has_feature(vq, VHOST_NET_F_VIRTIO_NET_HDR);
>  	hdr_size = nvq->vhost_hlen;
>  	zcopy = nvq->ubufs;
>  
> @@ -406,6 +416,8 @@ static void handle_tx(struct vhost_net *net)
>  			break;
>  		}
>  
> +		if (!has_vnet_hdr && vq->byteswap)
> +			virtio_net_hdr_swap((void *) vq->iov[0].iov_base);
>  		zcopy_used = zcopy && len >= VHOST_GOODCOPY_LEN
>  				   && (nvq->upend_idx + 1) % UIO_MAXIOV !>  				      nvq->done_idx
> @@ -570,7 +582,7 @@ static void handle_rx(struct vhost_net *net)
>  		.hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE
>  	};
>  	size_t total_len = 0;
> -	int err, mergeable;
> +	int err, mergeable, has_vnet_hdr;
>  	s16 headcount;
>  	size_t vhost_hlen, sock_hlen;
>  	size_t vhost_len, sock_len;
> @@ -588,6 +600,7 @@ static void handle_rx(struct vhost_net *net)
>  	vq_log = unlikely(vhost_has_feature(vq, VHOST_F_LOG_ALL)) ?
>  		vq->log : NULL;
>  	mergeable = vhost_has_feature(vq, VIRTIO_NET_F_MRG_RXBUF);
> +	has_vnet_hdr = vhost_has_feature(vq, VHOST_NET_F_VIRTIO_NET_HDR);
>  
>  	while ((sock_len = peek_head_len(sock->sk))) {
>  		sock_len += sock_hlen;
> @@ -638,6 +651,9 @@ static void handle_rx(struct vhost_net *net)
>  			vhost_discard_vq_desc(vq, headcount);
>  			continue;
>  		}
> +
> +		if (!has_vnet_hdr && vq->byteswap)
> +			virtio_net_hdr_swap((void *) vq->iov[0].iov_base);
>  		if (unlikely(vhost_hlen) &&
>  		    memcpy_toiovecend(nvq->hdr, (unsigned char *)&hdr, 0,
>  				      vhost_hlen)) {
> @@ -646,13 +662,18 @@ static void handle_rx(struct vhost_net *net)
>  			break;
>  		}
>  		/* TODO: Should check and handle checksum. */
> -		if (likely(mergeable) &&
> -		    memcpy_toiovecend(nvq->hdr, (unsigned char *)&headcount,
> -				      offsetof(typeof(hdr), num_buffers),
> -				      sizeof hdr.num_buffers)) {
> -			vq_err(vq, "Failed num_buffers write");
> -			vhost_discard_vq_desc(vq, headcount);
> -			break;
> +		if (likely(mergeable)) {
> +			__u16 tmp = headcount;
> +
> +			if (vq->byteswap)
> +				tmp = swab16(headcount);
> +			if (memcpy_toiovecend(nvq->hdr, (unsigned char *)&tmp,
> +					offsetof(typeof(hdr), num_buffers),
> +					      sizeof(hdr.num_buffers))) {
> +				vq_err(vq, "Failed num_buffers write");
> +				vhost_discard_vq_desc(vq, headcount);
> +				break;
> +			}
>  		}
>  		vhost_add_used_and_signal_n(&net->dev, vq, vq->heads,
>  					    headcount);
> -- 
> 1.7.10.4

WARNING: multiple messages have this Message-ID (diff)
From: "Michael S. Tsirkin" <mst@redhat.com>
To: "Cédric Le Goater" <clg@fr.ibm.com>
Cc: kvm@vger.kernel.org, kvm-ppc@vger.kernel.org, agraf@suse.de,
	paulus@samba.org, gkurz@linux.vnet.ibm.com.patch, aik@ozlabs.ru
Subject: Re: [RFC PATCH 4/4] vhost_net: byteswap virtio_net header
Date: Mon, 3 Nov 2014 18:25:25 +0200	[thread overview]
Message-ID: <20141103162525.GC24877@redhat.com> (raw)
In-Reply-To: <1414571925-16918-5-git-send-email-clg@fr.ibm.com>

On Wed, Oct 29, 2014 at 09:38:45AM +0100, Cédric Le Goater wrote:
> Signed-off-by: Cédric Le Goater <clg@fr.ibm.com>

This patch casts userspace pointers to void *,
this is generally unsafe. In particular sparse will warn.

> ---
>  drivers/vhost/net.c |   39 ++++++++++++++++++++++++++++++---------
>  1 file changed, 30 insertions(+), 9 deletions(-)
> 
> diff --git a/drivers/vhost/net.c b/drivers/vhost/net.c
> index 8dae2f724a35..f2d5f585dae9 100644
> --- a/drivers/vhost/net.c
> +++ b/drivers/vhost/net.c
> @@ -18,6 +18,7 @@
>  #include <linux/file.h>
>  #include <linux/slab.h>
>  #include <linux/vmalloc.h>
> +#include <linux/swab.h>
>  
>  #include <linux/net.h>
>  #include <linux/if_packet.h>
> @@ -329,6 +330,14 @@ static void vhost_zerocopy_callback(struct ubuf_info *ubuf, bool success)
>  	rcu_read_unlock_bh();
>  }
>  
> +static void virtio_net_hdr_swap(struct virtio_net_hdr *hdr)
> +{
> +	hdr->hdr_len     = swab16(hdr->hdr_len);
> +	hdr->gso_size    = swab16(hdr->gso_size);
> +	hdr->csum_start  = swab16(hdr->csum_start);
> +	hdr->csum_offset = swab16(hdr->csum_offset);
> +}
> +
>  /* Expects to be always run from workqueue - which acts as
>   * read-size critical section for our kind of RCU. */
>  static void handle_tx(struct vhost_net *net)
> @@ -346,7 +355,7 @@ static void handle_tx(struct vhost_net *net)
>  		.msg_flags = MSG_DONTWAIT,
>  	};
>  	size_t len, total_len = 0;
> -	int err;
> +	int err, has_vnet_hdr;
>  	size_t hdr_size;
>  	struct socket *sock;
>  	struct vhost_net_ubuf_ref *uninitialized_var(ubufs);
> @@ -359,6 +368,7 @@ static void handle_tx(struct vhost_net *net)
>  
>  	vhost_disable_notify(&net->dev, vq);
>  
> +	has_vnet_hdr = vhost_has_feature(vq, VHOST_NET_F_VIRTIO_NET_HDR);
>  	hdr_size = nvq->vhost_hlen;
>  	zcopy = nvq->ubufs;
>  
> @@ -406,6 +416,8 @@ static void handle_tx(struct vhost_net *net)
>  			break;
>  		}
>  
> +		if (!has_vnet_hdr && vq->byteswap)
> +			virtio_net_hdr_swap((void *) vq->iov[0].iov_base);
>  		zcopy_used = zcopy && len >= VHOST_GOODCOPY_LEN
>  				   && (nvq->upend_idx + 1) % UIO_MAXIOV !=
>  				      nvq->done_idx
> @@ -570,7 +582,7 @@ static void handle_rx(struct vhost_net *net)
>  		.hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE
>  	};
>  	size_t total_len = 0;
> -	int err, mergeable;
> +	int err, mergeable, has_vnet_hdr;
>  	s16 headcount;
>  	size_t vhost_hlen, sock_hlen;
>  	size_t vhost_len, sock_len;
> @@ -588,6 +600,7 @@ static void handle_rx(struct vhost_net *net)
>  	vq_log = unlikely(vhost_has_feature(vq, VHOST_F_LOG_ALL)) ?
>  		vq->log : NULL;
>  	mergeable = vhost_has_feature(vq, VIRTIO_NET_F_MRG_RXBUF);
> +	has_vnet_hdr = vhost_has_feature(vq, VHOST_NET_F_VIRTIO_NET_HDR);
>  
>  	while ((sock_len = peek_head_len(sock->sk))) {
>  		sock_len += sock_hlen;
> @@ -638,6 +651,9 @@ static void handle_rx(struct vhost_net *net)
>  			vhost_discard_vq_desc(vq, headcount);
>  			continue;
>  		}
> +
> +		if (!has_vnet_hdr && vq->byteswap)
> +			virtio_net_hdr_swap((void *) vq->iov[0].iov_base);
>  		if (unlikely(vhost_hlen) &&
>  		    memcpy_toiovecend(nvq->hdr, (unsigned char *)&hdr, 0,
>  				      vhost_hlen)) {
> @@ -646,13 +662,18 @@ static void handle_rx(struct vhost_net *net)
>  			break;
>  		}
>  		/* TODO: Should check and handle checksum. */
> -		if (likely(mergeable) &&
> -		    memcpy_toiovecend(nvq->hdr, (unsigned char *)&headcount,
> -				      offsetof(typeof(hdr), num_buffers),
> -				      sizeof hdr.num_buffers)) {
> -			vq_err(vq, "Failed num_buffers write");
> -			vhost_discard_vq_desc(vq, headcount);
> -			break;
> +		if (likely(mergeable)) {
> +			__u16 tmp = headcount;
> +
> +			if (vq->byteswap)
> +				tmp = swab16(headcount);
> +			if (memcpy_toiovecend(nvq->hdr, (unsigned char *)&tmp,
> +					offsetof(typeof(hdr), num_buffers),
> +					      sizeof(hdr.num_buffers))) {
> +				vq_err(vq, "Failed num_buffers write");
> +				vhost_discard_vq_desc(vq, headcount);
> +				break;
> +			}
>  		}
>  		vhost_add_used_and_signal_n(&net->dev, vq, vq->heads,
>  					    headcount);
> -- 
> 1.7.10.4

  reply	other threads:[~2014-11-03 16:25 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-29  8:38 [RFC PATCH 0/4] vhost_net: support for cross endian guests Cédric Le Goater
2014-10-29  8:38 ` Cédric Le Goater
2014-10-29  8:38 ` [RFC PATCH 1/4] vhost: add VHOST_VRING_F_BYTESWAP flag Cédric Le Goater
2014-10-29  8:38   ` Cédric Le Goater
2014-11-03 16:28   ` Michael S. Tsirkin
2014-11-03 16:28     ` Michael S. Tsirkin
2014-10-29  8:38 ` [RFC PATCH 2/4] vhost: add byteswap routines Cédric Le Goater
2014-10-29  8:38   ` Cédric Le Goater
2014-11-03 15:38   ` Cornelia Huck
2014-11-03 15:38     ` Cornelia Huck
2014-10-29  8:38 ` [RFC PATCH 3/4] vhost: byteswap virtqueue attributes Cédric Le Goater
2014-10-29  8:38   ` Cédric Le Goater
2014-11-03 16:02   ` Cornelia Huck
2014-11-03 16:02     ` Cornelia Huck
2014-10-29  8:38 ` [RFC PATCH 4/4] vhost_net: byteswap virtio_net header Cédric Le Goater
2014-10-29  8:38   ` Cédric Le Goater
2014-11-03 16:25   ` Michael S. Tsirkin [this message]
2014-11-03 16:25     ` Michael S. Tsirkin
2014-11-03 16:23 ` [RFC PATCH 0/4] vhost_net: support for cross endian guests Michael S. Tsirkin
2014-11-03 16:23   ` Michael S. Tsirkin
2014-11-04  8:07   ` Cedric Le Goater
2014-11-04  8:07     ` Cedric Le Goater

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=20141103162525.GC24877@redhat.com \
    --to=mst@redhat.com \
    --cc=agraf@suse.de \
    --cc=aik@ozlabs.ru \
    --cc=clg@fr.ibm.com \
    --cc=gkurz@linux.vnet.ibm.com.patch \
    --cc=kvm-ppc@vger.kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=paulus@samba.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.