Linux-remoteproc Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH] rpmsg: virtio: add endianness conversions
@ 2020-06-08 12:39 Guennadi Liakhovetski
  2020-06-08 13:13 ` Michael S. Tsirkin
  2020-06-15 20:47 ` Mathieu Poirier
  0 siblings, 2 replies; 21+ messages in thread
From: Guennadi Liakhovetski @ 2020-06-08 12:39 UTC (permalink / raw)
  To: linux-remoteproc
  Cc: Michael S. Tsirkin, Ohad Ben-Cohen, Bjorn Andersson,
	Pierre-Louis Bossart, Liam Girdwood

According to the VirtIO 1.0 spec data, sent over virtual queues must
be in little-endian format. Update the RPMsg VirtIO implementation
to enforce that.

Suggested-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
---
 drivers/rpmsg/virtio_rpmsg_bus.c | 61 ++++++++++++++++++++++------------------
 1 file changed, 33 insertions(+), 28 deletions(-)

diff --git a/drivers/rpmsg/virtio_rpmsg_bus.c b/drivers/rpmsg/virtio_rpmsg_bus.c
index 07d4f33..b8ff42b 100644
--- a/drivers/rpmsg/virtio_rpmsg_bus.c
+++ b/drivers/rpmsg/virtio_rpmsg_bus.c
@@ -11,6 +11,8 @@
 
 #define pr_fmt(fmt) "%s: " fmt, __func__
 
+#include <asm/byteorder.h>
+
 #include <linux/dma-mapping.h>
 #include <linux/idr.h>
 #include <linux/jiffies.h>
@@ -22,6 +24,7 @@
 #include <linux/scatterlist.h>
 #include <linux/slab.h>
 #include <linux/sched.h>
+#include <linux/types.h>
 #include <linux/virtio.h>
 #include <linux/virtio_ids.h>
 #include <linux/virtio_config.h>
@@ -84,11 +87,11 @@ struct virtproc_info {
  * Every message sent(/received) on the rpmsg bus begins with this header.
  */
 struct rpmsg_hdr {
-	u32 src;
-	u32 dst;
-	u32 reserved;
-	u16 len;
-	u16 flags;
+	__le32 src;
+	__le32 dst;
+	__le32 reserved;
+	__le16 len;
+	__le16 flags;
 	u8 data[];
 } __packed;
 
@@ -106,8 +109,8 @@ struct rpmsg_hdr {
  */
 struct rpmsg_ns_msg {
 	char name[RPMSG_NAME_SIZE];
-	u32 addr;
-	u32 flags;
+	__le32 addr;
+	__le32 flags;
 } __packed;
 
 /**
@@ -335,8 +338,8 @@ static int virtio_rpmsg_announce_create(struct rpmsg_device *rpdev)
 		struct rpmsg_ns_msg nsm;
 
 		strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
-		nsm.addr = rpdev->ept->addr;
-		nsm.flags = RPMSG_NS_CREATE;
+		nsm.addr = cpu_to_le32(rpdev->ept->addr);
+		nsm.flags = cpu_to_le32(RPMSG_NS_CREATE);
 
 		err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
 		if (err)
@@ -359,8 +362,8 @@ static int virtio_rpmsg_announce_destroy(struct rpmsg_device *rpdev)
 		struct rpmsg_ns_msg nsm;
 
 		strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
-		nsm.addr = rpdev->ept->addr;
-		nsm.flags = RPMSG_NS_DESTROY;
+		nsm.addr = cpu_to_le32(rpdev->ept->addr);
+		nsm.flags = cpu_to_le32(RPMSG_NS_DESTROY);
 
 		err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
 		if (err)
@@ -612,15 +615,15 @@ static int rpmsg_send_offchannel_raw(struct rpmsg_device *rpdev,
 		}
 	}
 
-	msg->len = len;
+	msg->len = cpu_to_le16(len);
 	msg->flags = 0;
-	msg->src = src;
-	msg->dst = dst;
+	msg->src = cpu_to_le32(src);
+	msg->dst = cpu_to_le32(dst);
 	msg->reserved = 0;
 	memcpy(msg->data, data, len);
 
 	dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
-		msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
+		src, dst, len, msg->flags, msg->reserved);
 #if defined(CONFIG_DYNAMIC_DEBUG)
 	dynamic_hex_dump("rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
 			 msg, sizeof(*msg) + msg->len, true);
@@ -704,13 +707,15 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
 {
 	struct rpmsg_endpoint *ept;
 	struct scatterlist sg;
+	unsigned int msg_len = le16_to_cpu(msg->len);
 	int err;
 
 	dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
-		msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
+		le32_to_cpu(msg->src), le32_to_cpu(msg->dst), msg_len,
+		le16_to_cpu(msg->flags), le32_to_cpu(msg->reserved));
 #if defined(CONFIG_DYNAMIC_DEBUG)
 	dynamic_hex_dump("rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
-			 msg, sizeof(*msg) + msg->len, true);
+			 msg, sizeof(*msg) + msg_len, true);
 #endif
 
 	/*
@@ -718,15 +723,15 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
 	 * the reported payload length.
 	 */
 	if (len > vrp->buf_size ||
-	    msg->len > (len - sizeof(struct rpmsg_hdr))) {
-		dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg->len);
+	    msg_len > (len - sizeof(struct rpmsg_hdr))) {
+		dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg_len);
 		return -EINVAL;
 	}
 
 	/* use the dst addr to fetch the callback of the appropriate user */
 	mutex_lock(&vrp->endpoints_lock);
 
-	ept = idr_find(&vrp->endpoints, msg->dst);
+	ept = idr_find(&vrp->endpoints, le32_to_cpu(msg->dst));
 
 	/* let's make sure no one deallocates ept while we use it */
 	if (ept)
@@ -739,8 +744,8 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
 		mutex_lock(&ept->cb_lock);
 
 		if (ept->cb)
-			ept->cb(ept->rpdev, msg->data, msg->len, ept->priv,
-				msg->src);
+			ept->cb(ept->rpdev, msg->data, msg_len, ept->priv,
+				le32_to_cpu(msg->src));
 
 		mutex_unlock(&ept->cb_lock);
 
@@ -846,15 +851,15 @@ static int rpmsg_ns_cb(struct rpmsg_device *rpdev, void *data, int len,
 	/* don't trust the remote processor for null terminating the name */
 	msg->name[RPMSG_NAME_SIZE - 1] = '\0';
 
-	dev_info(dev, "%sing channel %s addr 0x%x\n",
-		 msg->flags & RPMSG_NS_DESTROY ? "destroy" : "creat",
-		 msg->name, msg->addr);
-
 	strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
 	chinfo.src = RPMSG_ADDR_ANY;
-	chinfo.dst = msg->addr;
+	chinfo.dst = le32_to_cpu(msg->addr);
+
+	dev_info(dev, "%sing channel %s addr 0x%x\n",
+		 le32_to_cpu(msg->flags) & RPMSG_NS_DESTROY ? "destroy" : "creat",
+		 msg->name, chinfo.dst);
 
-	if (msg->flags & RPMSG_NS_DESTROY) {
+	if (le32_to_cpu(msg->flags) & RPMSG_NS_DESTROY) {
 		ret = rpmsg_unregister_device(&vrp->vdev->dev, &chinfo);
 		if (ret)
 			dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
-- 
1.9.3


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH] rpmsg: virtio: add endianness conversions
  2020-06-08 12:39 [PATCH] rpmsg: virtio: add endianness conversions Guennadi Liakhovetski
@ 2020-06-08 13:13 ` Michael S. Tsirkin
  2020-06-15 20:47 ` Mathieu Poirier
  1 sibling, 0 replies; 21+ messages in thread
From: Michael S. Tsirkin @ 2020-06-08 13:13 UTC (permalink / raw)
  To: Guennadi Liakhovetski
  Cc: linux-remoteproc, Ohad Ben-Cohen, Bjorn Andersson,
	Pierre-Louis Bossart, Liam Girdwood

On Mon, Jun 08, 2020 at 02:39:32PM +0200, Guennadi Liakhovetski wrote:
> According to the VirtIO 1.0 spec data, sent over virtual queues must
> be in little-endian format. Update the RPMsg VirtIO implementation
> to enforce that.
> 
> Suggested-by: Michael S. Tsirkin <mst@redhat.com>
> Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>

> ---
>  drivers/rpmsg/virtio_rpmsg_bus.c | 61 ++++++++++++++++++++++------------------
>  1 file changed, 33 insertions(+), 28 deletions(-)
> 
> diff --git a/drivers/rpmsg/virtio_rpmsg_bus.c b/drivers/rpmsg/virtio_rpmsg_bus.c
> index 07d4f33..b8ff42b 100644
> --- a/drivers/rpmsg/virtio_rpmsg_bus.c
> +++ b/drivers/rpmsg/virtio_rpmsg_bus.c
> @@ -11,6 +11,8 @@
>  
>  #define pr_fmt(fmt) "%s: " fmt, __func__
>  
> +#include <asm/byteorder.h>
> +
>  #include <linux/dma-mapping.h>
>  #include <linux/idr.h>
>  #include <linux/jiffies.h>
> @@ -22,6 +24,7 @@
>  #include <linux/scatterlist.h>
>  #include <linux/slab.h>
>  #include <linux/sched.h>
> +#include <linux/types.h>
>  #include <linux/virtio.h>
>  #include <linux/virtio_ids.h>
>  #include <linux/virtio_config.h>
> @@ -84,11 +87,11 @@ struct virtproc_info {
>   * Every message sent(/received) on the rpmsg bus begins with this header.
>   */
>  struct rpmsg_hdr {
> -	u32 src;
> -	u32 dst;
> -	u32 reserved;
> -	u16 len;
> -	u16 flags;
> +	__le32 src;
> +	__le32 dst;
> +	__le32 reserved;
> +	__le16 len;
> +	__le16 flags;
>  	u8 data[];
>  } __packed;
>

I really has __virtio32/virtio16  types in mind.
This way existing BE setups (if any!) can keep working.

However if RPMSG maintainers want this, that's also fine
by me ...


  
> @@ -106,8 +109,8 @@ struct rpmsg_hdr {
>   */
>  struct rpmsg_ns_msg {
>  	char name[RPMSG_NAME_SIZE];
> -	u32 addr;
> -	u32 flags;
> +	__le32 addr;
> +	__le32 flags;
>  } __packed;
>  
>  /**
> @@ -335,8 +338,8 @@ static int virtio_rpmsg_announce_create(struct rpmsg_device *rpdev)
>  		struct rpmsg_ns_msg nsm;
>  
>  		strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
> -		nsm.addr = rpdev->ept->addr;
> -		nsm.flags = RPMSG_NS_CREATE;
> +		nsm.addr = cpu_to_le32(rpdev->ept->addr);
> +		nsm.flags = cpu_to_le32(RPMSG_NS_CREATE);
>  
>  		err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
>  		if (err)
> @@ -359,8 +362,8 @@ static int virtio_rpmsg_announce_destroy(struct rpmsg_device *rpdev)
>  		struct rpmsg_ns_msg nsm;
>  
>  		strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
> -		nsm.addr = rpdev->ept->addr;
> -		nsm.flags = RPMSG_NS_DESTROY;
> +		nsm.addr = cpu_to_le32(rpdev->ept->addr);
> +		nsm.flags = cpu_to_le32(RPMSG_NS_DESTROY);
>  
>  		err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
>  		if (err)
> @@ -612,15 +615,15 @@ static int rpmsg_send_offchannel_raw(struct rpmsg_device *rpdev,
>  		}
>  	}
>  
> -	msg->len = len;
> +	msg->len = cpu_to_le16(len);
>  	msg->flags = 0;
> -	msg->src = src;
> -	msg->dst = dst;
> +	msg->src = cpu_to_le32(src);
> +	msg->dst = cpu_to_le32(dst);
>  	msg->reserved = 0;
>  	memcpy(msg->data, data, len);
>  
>  	dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
> -		msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
> +		src, dst, len, msg->flags, msg->reserved);
>  #if defined(CONFIG_DYNAMIC_DEBUG)
>  	dynamic_hex_dump("rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
>  			 msg, sizeof(*msg) + msg->len, true);
> @@ -704,13 +707,15 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
>  {
>  	struct rpmsg_endpoint *ept;
>  	struct scatterlist sg;
> +	unsigned int msg_len = le16_to_cpu(msg->len);
>  	int err;
>  
>  	dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
> -		msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
> +		le32_to_cpu(msg->src), le32_to_cpu(msg->dst), msg_len,
> +		le16_to_cpu(msg->flags), le32_to_cpu(msg->reserved));
>  #if defined(CONFIG_DYNAMIC_DEBUG)
>  	dynamic_hex_dump("rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
> -			 msg, sizeof(*msg) + msg->len, true);
> +			 msg, sizeof(*msg) + msg_len, true);
>  #endif
>  
>  	/*
> @@ -718,15 +723,15 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
>  	 * the reported payload length.
>  	 */
>  	if (len > vrp->buf_size ||
> -	    msg->len > (len - sizeof(struct rpmsg_hdr))) {
> -		dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg->len);
> +	    msg_len > (len - sizeof(struct rpmsg_hdr))) {
> +		dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg_len);
>  		return -EINVAL;
>  	}
>  
>  	/* use the dst addr to fetch the callback of the appropriate user */
>  	mutex_lock(&vrp->endpoints_lock);
>  
> -	ept = idr_find(&vrp->endpoints, msg->dst);
> +	ept = idr_find(&vrp->endpoints, le32_to_cpu(msg->dst));
>  
>  	/* let's make sure no one deallocates ept while we use it */
>  	if (ept)
> @@ -739,8 +744,8 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
>  		mutex_lock(&ept->cb_lock);
>  
>  		if (ept->cb)
> -			ept->cb(ept->rpdev, msg->data, msg->len, ept->priv,
> -				msg->src);
> +			ept->cb(ept->rpdev, msg->data, msg_len, ept->priv,
> +				le32_to_cpu(msg->src));
>  
>  		mutex_unlock(&ept->cb_lock);
>  
> @@ -846,15 +851,15 @@ static int rpmsg_ns_cb(struct rpmsg_device *rpdev, void *data, int len,
>  	/* don't trust the remote processor for null terminating the name */
>  	msg->name[RPMSG_NAME_SIZE - 1] = '\0';
>  
> -	dev_info(dev, "%sing channel %s addr 0x%x\n",
> -		 msg->flags & RPMSG_NS_DESTROY ? "destroy" : "creat",
> -		 msg->name, msg->addr);
> -
>  	strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
>  	chinfo.src = RPMSG_ADDR_ANY;
> -	chinfo.dst = msg->addr;
> +	chinfo.dst = le32_to_cpu(msg->addr);
> +
> +	dev_info(dev, "%sing channel %s addr 0x%x\n",
> +		 le32_to_cpu(msg->flags) & RPMSG_NS_DESTROY ? "destroy" : "creat",
> +		 msg->name, chinfo.dst);
>  
> -	if (msg->flags & RPMSG_NS_DESTROY) {
> +	if (le32_to_cpu(msg->flags) & RPMSG_NS_DESTROY) {
>  		ret = rpmsg_unregister_device(&vrp->vdev->dev, &chinfo);
>  		if (ret)
>  			dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
> -- 
> 1.9.3


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH] rpmsg: virtio: add endianness conversions
  2020-06-08 12:39 [PATCH] rpmsg: virtio: add endianness conversions Guennadi Liakhovetski
  2020-06-08 13:13 ` Michael S. Tsirkin
@ 2020-06-15 20:47 ` Mathieu Poirier
  2020-06-16  6:14   ` Guennadi Liakhovetski
  1 sibling, 1 reply; 21+ messages in thread
From: Mathieu Poirier @ 2020-06-15 20:47 UTC (permalink / raw)
  To: Guennadi Liakhovetski
  Cc: linux-remoteproc, Michael S. Tsirkin, Ohad Ben-Cohen,
	Bjorn Andersson, Pierre-Louis Bossart, Liam Girdwood

On Mon, Jun 08, 2020 at 02:39:32PM +0200, Guennadi Liakhovetski wrote:
> According to the VirtIO 1.0 spec data, sent over virtual queues must
> be in little-endian format. Update the RPMsg VirtIO implementation
> to enforce that.
> 
> Suggested-by: Michael S. Tsirkin <mst@redhat.com>
> Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
> ---
>  drivers/rpmsg/virtio_rpmsg_bus.c | 61 ++++++++++++++++++++++------------------
>  1 file changed, 33 insertions(+), 28 deletions(-)
> 
> diff --git a/drivers/rpmsg/virtio_rpmsg_bus.c b/drivers/rpmsg/virtio_rpmsg_bus.c
> index 07d4f33..b8ff42b 100644
> --- a/drivers/rpmsg/virtio_rpmsg_bus.c
> +++ b/drivers/rpmsg/virtio_rpmsg_bus.c
> @@ -11,6 +11,8 @@
>  
>  #define pr_fmt(fmt) "%s: " fmt, __func__
>  
> +#include <asm/byteorder.h>
> +
>  #include <linux/dma-mapping.h>
>  #include <linux/idr.h>
>  #include <linux/jiffies.h>
> @@ -22,6 +24,7 @@
>  #include <linux/scatterlist.h>
>  #include <linux/slab.h>
>  #include <linux/sched.h>
> +#include <linux/types.h>
>  #include <linux/virtio.h>
>  #include <linux/virtio_ids.h>
>  #include <linux/virtio_config.h>
> @@ -84,11 +87,11 @@ struct virtproc_info {
>   * Every message sent(/received) on the rpmsg bus begins with this header.
>   */
>  struct rpmsg_hdr {
> -	u32 src;
> -	u32 dst;
> -	u32 reserved;
> -	u16 len;
> -	u16 flags;
> +	__le32 src;
> +	__le32 dst;
> +	__le32 reserved;
> +	__le16 len;
> +	__le16 flags;
>  	u8 data[];
>  } __packed;
>  
> @@ -106,8 +109,8 @@ struct rpmsg_hdr {
>   */
>  struct rpmsg_ns_msg {
>  	char name[RPMSG_NAME_SIZE];
> -	u32 addr;
> -	u32 flags;
> +	__le32 addr;
> +	__le32 flags;
>  } __packed;
>  
>  /**
> @@ -335,8 +338,8 @@ static int virtio_rpmsg_announce_create(struct rpmsg_device *rpdev)
>  		struct rpmsg_ns_msg nsm;
>  
>  		strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
> -		nsm.addr = rpdev->ept->addr;
> -		nsm.flags = RPMSG_NS_CREATE;
> +		nsm.addr = cpu_to_le32(rpdev->ept->addr);
> +		nsm.flags = cpu_to_le32(RPMSG_NS_CREATE);

So how will this work with existing firmware?  

>  
>  		err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
>  		if (err)
> @@ -359,8 +362,8 @@ static int virtio_rpmsg_announce_destroy(struct rpmsg_device *rpdev)
>  		struct rpmsg_ns_msg nsm;
>  
>  		strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
> -		nsm.addr = rpdev->ept->addr;
> -		nsm.flags = RPMSG_NS_DESTROY;
> +		nsm.addr = cpu_to_le32(rpdev->ept->addr);
> +		nsm.flags = cpu_to_le32(RPMSG_NS_DESTROY);
>  
>  		err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
>  		if (err)
> @@ -612,15 +615,15 @@ static int rpmsg_send_offchannel_raw(struct rpmsg_device *rpdev,
>  		}
>  	}
>  
> -	msg->len = len;
> +	msg->len = cpu_to_le16(len);
>  	msg->flags = 0;
> -	msg->src = src;
> -	msg->dst = dst;
> +	msg->src = cpu_to_le32(src);
> +	msg->dst = cpu_to_le32(dst);
>  	msg->reserved = 0;
>  	memcpy(msg->data, data, len);
>  
>  	dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
> -		msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
> +		src, dst, len, msg->flags, msg->reserved);
>  #if defined(CONFIG_DYNAMIC_DEBUG)
>  	dynamic_hex_dump("rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
>  			 msg, sizeof(*msg) + msg->len, true);
> @@ -704,13 +707,15 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
>  {
>  	struct rpmsg_endpoint *ept;
>  	struct scatterlist sg;
> +	unsigned int msg_len = le16_to_cpu(msg->len);

And here too, along with everything else that follows?

As Michael suggested using __virtioXY types would allow to use the
virtioXY_to_cpu() and cpu_to_virtioXY() macros, which is definitely a step in
the right direction.

Thanks,
Mathieu

>  	int err;
>  
>  	dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
> -		msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
> +		le32_to_cpu(msg->src), le32_to_cpu(msg->dst), msg_len,
> +		le16_to_cpu(msg->flags), le32_to_cpu(msg->reserved));
>  #if defined(CONFIG_DYNAMIC_DEBUG)
>  	dynamic_hex_dump("rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
> -			 msg, sizeof(*msg) + msg->len, true);
> +			 msg, sizeof(*msg) + msg_len, true);
>  #endif
>  
>  	/*
> @@ -718,15 +723,15 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
>  	 * the reported payload length.
>  	 */
>  	if (len > vrp->buf_size ||
> -	    msg->len > (len - sizeof(struct rpmsg_hdr))) {
> -		dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg->len);
> +	    msg_len > (len - sizeof(struct rpmsg_hdr))) {
> +		dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg_len);
>  		return -EINVAL;
>  	}
>  
>  	/* use the dst addr to fetch the callback of the appropriate user */
>  	mutex_lock(&vrp->endpoints_lock);
>  
> -	ept = idr_find(&vrp->endpoints, msg->dst);
> +	ept = idr_find(&vrp->endpoints, le32_to_cpu(msg->dst));
>  
>  	/* let's make sure no one deallocates ept while we use it */
>  	if (ept)
> @@ -739,8 +744,8 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
>  		mutex_lock(&ept->cb_lock);
>  
>  		if (ept->cb)
> -			ept->cb(ept->rpdev, msg->data, msg->len, ept->priv,
> -				msg->src);
> +			ept->cb(ept->rpdev, msg->data, msg_len, ept->priv,
> +				le32_to_cpu(msg->src));
>  
>  		mutex_unlock(&ept->cb_lock);
>  
> @@ -846,15 +851,15 @@ static int rpmsg_ns_cb(struct rpmsg_device *rpdev, void *data, int len,
>  	/* don't trust the remote processor for null terminating the name */
>  	msg->name[RPMSG_NAME_SIZE - 1] = '\0';
>  
> -	dev_info(dev, "%sing channel %s addr 0x%x\n",
> -		 msg->flags & RPMSG_NS_DESTROY ? "destroy" : "creat",
> -		 msg->name, msg->addr);
> -
>  	strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
>  	chinfo.src = RPMSG_ADDR_ANY;
> -	chinfo.dst = msg->addr;
> +	chinfo.dst = le32_to_cpu(msg->addr);
> +
> +	dev_info(dev, "%sing channel %s addr 0x%x\n",
> +		 le32_to_cpu(msg->flags) & RPMSG_NS_DESTROY ? "destroy" : "creat",
> +		 msg->name, chinfo.dst);
>  
> -	if (msg->flags & RPMSG_NS_DESTROY) {
> +	if (le32_to_cpu(msg->flags) & RPMSG_NS_DESTROY) {
>  		ret = rpmsg_unregister_device(&vrp->vdev->dev, &chinfo);
>  		if (ret)
>  			dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
> -- 
> 1.9.3
> 

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH] rpmsg: virtio: add endianness conversions
  2020-06-15 20:47 ` Mathieu Poirier
@ 2020-06-16  6:14   ` Guennadi Liakhovetski
  2020-06-16  7:30     ` [PATCH v2] " Guennadi Liakhovetski
  0 siblings, 1 reply; 21+ messages in thread
From: Guennadi Liakhovetski @ 2020-06-16  6:14 UTC (permalink / raw)
  To: Mathieu Poirier
  Cc: linux-remoteproc, Michael S. Tsirkin, Ohad Ben-Cohen,
	Bjorn Andersson, Pierre-Louis Bossart, Liam Girdwood

Hi Mathieu,

Thanks for your review.

On Mon, Jun 15, 2020 at 02:47:07PM -0600, Mathieu Poirier wrote:
> On Mon, Jun 08, 2020 at 02:39:32PM +0200, Guennadi Liakhovetski wrote:
> > According to the VirtIO 1.0 spec data, sent over virtual queues must
> > be in little-endian format. Update the RPMsg VirtIO implementation
> > to enforce that.
> > 
> > Suggested-by: Michael S. Tsirkin <mst@redhat.com>
> > Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
> > ---
> >  drivers/rpmsg/virtio_rpmsg_bus.c | 61 ++++++++++++++++++++++------------------
> >  1 file changed, 33 insertions(+), 28 deletions(-)
> > 
> > diff --git a/drivers/rpmsg/virtio_rpmsg_bus.c b/drivers/rpmsg/virtio_rpmsg_bus.c
> > index 07d4f33..b8ff42b 100644
> > --- a/drivers/rpmsg/virtio_rpmsg_bus.c
> > +++ b/drivers/rpmsg/virtio_rpmsg_bus.c
> > @@ -11,6 +11,8 @@
> >  
> >  #define pr_fmt(fmt) "%s: " fmt, __func__
> >  
> > +#include <asm/byteorder.h>
> > +
> >  #include <linux/dma-mapping.h>
> >  #include <linux/idr.h>
> >  #include <linux/jiffies.h>
> > @@ -22,6 +24,7 @@
> >  #include <linux/scatterlist.h>
> >  #include <linux/slab.h>
> >  #include <linux/sched.h>
> > +#include <linux/types.h>
> >  #include <linux/virtio.h>
> >  #include <linux/virtio_ids.h>
> >  #include <linux/virtio_config.h>
> > @@ -84,11 +87,11 @@ struct virtproc_info {
> >   * Every message sent(/received) on the rpmsg bus begins with this header.
> >   */
> >  struct rpmsg_hdr {
> > -	u32 src;
> > -	u32 dst;
> > -	u32 reserved;
> > -	u16 len;
> > -	u16 flags;
> > +	__le32 src;
> > +	__le32 dst;
> > +	__le32 reserved;
> > +	__le16 len;
> > +	__le16 flags;
> >  	u8 data[];
> >  } __packed;
> >  
> > @@ -106,8 +109,8 @@ struct rpmsg_hdr {
> >   */
> >  struct rpmsg_ns_msg {
> >  	char name[RPMSG_NAME_SIZE];
> > -	u32 addr;
> > -	u32 flags;
> > +	__le32 addr;
> > +	__le32 flags;
> >  } __packed;
> >  
> >  /**
> > @@ -335,8 +338,8 @@ static int virtio_rpmsg_announce_create(struct rpmsg_device *rpdev)
> >  		struct rpmsg_ns_msg nsm;
> >  
> >  		strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
> > -		nsm.addr = rpdev->ept->addr;
> > -		nsm.flags = RPMSG_NS_CREATE;
> > +		nsm.addr = cpu_to_le32(rpdev->ept->addr);
> > +		nsm.flags = cpu_to_le32(RPMSG_NS_CREATE);
> 
> So how will this work with existing firmware?  

Is this currently running on any BE machines?

> >  		err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
> >  		if (err)
> > @@ -359,8 +362,8 @@ static int virtio_rpmsg_announce_destroy(struct rpmsg_device *rpdev)
> >  		struct rpmsg_ns_msg nsm;
> >  
> >  		strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
> > -		nsm.addr = rpdev->ept->addr;
> > -		nsm.flags = RPMSG_NS_DESTROY;
> > +		nsm.addr = cpu_to_le32(rpdev->ept->addr);
> > +		nsm.flags = cpu_to_le32(RPMSG_NS_DESTROY);
> >  
> >  		err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
> >  		if (err)
> > @@ -612,15 +615,15 @@ static int rpmsg_send_offchannel_raw(struct rpmsg_device *rpdev,
> >  		}
> >  	}
> >  
> > -	msg->len = len;
> > +	msg->len = cpu_to_le16(len);
> >  	msg->flags = 0;
> > -	msg->src = src;
> > -	msg->dst = dst;
> > +	msg->src = cpu_to_le32(src);
> > +	msg->dst = cpu_to_le32(dst);
> >  	msg->reserved = 0;
> >  	memcpy(msg->data, data, len);
> >  
> >  	dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
> > -		msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
> > +		src, dst, len, msg->flags, msg->reserved);
> >  #if defined(CONFIG_DYNAMIC_DEBUG)
> >  	dynamic_hex_dump("rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
> >  			 msg, sizeof(*msg) + msg->len, true);
> > @@ -704,13 +707,15 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
> >  {
> >  	struct rpmsg_endpoint *ept;
> >  	struct scatterlist sg;
> > +	unsigned int msg_len = le16_to_cpu(msg->len);
> 
> And here too, along with everything else that follows?
> 
> As Michael suggested using __virtioXY types would allow to use the
> virtioXY_to_cpu() and cpu_to_virtioXY() macros, which is definitely a step in
> the right direction.

Ok, sure, I'll redo this, I just wanted to see if there would be any more comments 
before submitting a new version.

Thanks
Guennadi

> 
> Thanks,
> Mathieu
> 
> >  	int err;
> >  
> >  	dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
> > -		msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
> > +		le32_to_cpu(msg->src), le32_to_cpu(msg->dst), msg_len,
> > +		le16_to_cpu(msg->flags), le32_to_cpu(msg->reserved));
> >  #if defined(CONFIG_DYNAMIC_DEBUG)
> >  	dynamic_hex_dump("rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
> > -			 msg, sizeof(*msg) + msg->len, true);
> > +			 msg, sizeof(*msg) + msg_len, true);
> >  #endif
> >  
> >  	/*
> > @@ -718,15 +723,15 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
> >  	 * the reported payload length.
> >  	 */
> >  	if (len > vrp->buf_size ||
> > -	    msg->len > (len - sizeof(struct rpmsg_hdr))) {
> > -		dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg->len);
> > +	    msg_len > (len - sizeof(struct rpmsg_hdr))) {
> > +		dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg_len);
> >  		return -EINVAL;
> >  	}
> >  
> >  	/* use the dst addr to fetch the callback of the appropriate user */
> >  	mutex_lock(&vrp->endpoints_lock);
> >  
> > -	ept = idr_find(&vrp->endpoints, msg->dst);
> > +	ept = idr_find(&vrp->endpoints, le32_to_cpu(msg->dst));
> >  
> >  	/* let's make sure no one deallocates ept while we use it */
> >  	if (ept)
> > @@ -739,8 +744,8 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
> >  		mutex_lock(&ept->cb_lock);
> >  
> >  		if (ept->cb)
> > -			ept->cb(ept->rpdev, msg->data, msg->len, ept->priv,
> > -				msg->src);
> > +			ept->cb(ept->rpdev, msg->data, msg_len, ept->priv,
> > +				le32_to_cpu(msg->src));
> >  
> >  		mutex_unlock(&ept->cb_lock);
> >  
> > @@ -846,15 +851,15 @@ static int rpmsg_ns_cb(struct rpmsg_device *rpdev, void *data, int len,
> >  	/* don't trust the remote processor for null terminating the name */
> >  	msg->name[RPMSG_NAME_SIZE - 1] = '\0';
> >  
> > -	dev_info(dev, "%sing channel %s addr 0x%x\n",
> > -		 msg->flags & RPMSG_NS_DESTROY ? "destroy" : "creat",
> > -		 msg->name, msg->addr);
> > -
> >  	strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
> >  	chinfo.src = RPMSG_ADDR_ANY;
> > -	chinfo.dst = msg->addr;
> > +	chinfo.dst = le32_to_cpu(msg->addr);
> > +
> > +	dev_info(dev, "%sing channel %s addr 0x%x\n",
> > +		 le32_to_cpu(msg->flags) & RPMSG_NS_DESTROY ? "destroy" : "creat",
> > +		 msg->name, chinfo.dst);
> >  
> > -	if (msg->flags & RPMSG_NS_DESTROY) {
> > +	if (le32_to_cpu(msg->flags) & RPMSG_NS_DESTROY) {
> >  		ret = rpmsg_unregister_device(&vrp->vdev->dev, &chinfo);
> >  		if (ret)
> >  			dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
> > -- 
> > 1.9.3
> > 

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [PATCH v2] rpmsg: virtio: add endianness conversions
  2020-06-16  6:14   ` Guennadi Liakhovetski
@ 2020-06-16  7:30     ` Guennadi Liakhovetski
  2020-06-30 17:50       ` Mathieu Poirier
  0 siblings, 1 reply; 21+ messages in thread
From: Guennadi Liakhovetski @ 2020-06-16  7:30 UTC (permalink / raw)
  To: Mathieu Poirier
  Cc: linux-remoteproc, Michael S. Tsirkin, Ohad Ben-Cohen,
	Bjorn Andersson, Pierre-Louis Bossart, Liam Girdwood

According to the VirtIO 1.0 spec data, sent over virtual queues must
be in little-endian format. Update the RPMsg VirtIO implementation
to enforce that but let legacy configurations continue use native
endianness.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
---

v2: Following suggestions from Michael and Mathieu switch to using virtio16/32 
types and conversion functions.

 drivers/rpmsg/virtio_rpmsg_bus.c | 63 ++++++++++++++++++++++------------------
 1 file changed, 35 insertions(+), 28 deletions(-)

diff --git a/drivers/rpmsg/virtio_rpmsg_bus.c b/drivers/rpmsg/virtio_rpmsg_bus.c
index 07d4f33..41025df 100644
--- a/drivers/rpmsg/virtio_rpmsg_bus.c
+++ b/drivers/rpmsg/virtio_rpmsg_bus.c
@@ -11,6 +11,8 @@
 
 #define pr_fmt(fmt) "%s: " fmt, __func__
 
+#include <asm/byteorder.h>
+
 #include <linux/dma-mapping.h>
 #include <linux/idr.h>
 #include <linux/jiffies.h>
@@ -22,6 +24,7 @@
 #include <linux/scatterlist.h>
 #include <linux/slab.h>
 #include <linux/sched.h>
+#include <linux/types.h>
 #include <linux/virtio.h>
 #include <linux/virtio_ids.h>
 #include <linux/virtio_config.h>
@@ -84,11 +87,11 @@ struct virtproc_info {
  * Every message sent(/received) on the rpmsg bus begins with this header.
  */
 struct rpmsg_hdr {
-	u32 src;
-	u32 dst;
-	u32 reserved;
-	u16 len;
-	u16 flags;
+	__virtio32 src;
+	__virtio32 dst;
+	__virtio32 reserved;
+	__virtio16 len;
+	__virtio16 flags;
 	u8 data[];
 } __packed;
 
@@ -106,8 +109,8 @@ struct rpmsg_hdr {
  */
 struct rpmsg_ns_msg {
 	char name[RPMSG_NAME_SIZE];
-	u32 addr;
-	u32 flags;
+	__virtio32 addr;
+	__virtio32 flags;
 } __packed;
 
 /**
@@ -335,8 +338,8 @@ static int virtio_rpmsg_announce_create(struct rpmsg_device *rpdev)
 		struct rpmsg_ns_msg nsm;
 
 		strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
-		nsm.addr = rpdev->ept->addr;
-		nsm.flags = RPMSG_NS_CREATE;
+		nsm.addr = cpu_to_virtio32(vrp->vdev, rpdev->ept->addr);
+		nsm.flags = cpu_to_virtio32(vrp->vdev, RPMSG_NS_CREATE);
 
 		err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
 		if (err)
@@ -359,8 +362,8 @@ static int virtio_rpmsg_announce_destroy(struct rpmsg_device *rpdev)
 		struct rpmsg_ns_msg nsm;
 
 		strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
-		nsm.addr = rpdev->ept->addr;
-		nsm.flags = RPMSG_NS_DESTROY;
+		nsm.addr = cpu_to_virtio32(vrp->vdev, rpdev->ept->addr);
+		nsm.flags = cpu_to_virtio32(vrp->vdev, RPMSG_NS_DESTROY);
 
 		err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
 		if (err)
@@ -612,15 +615,15 @@ static int rpmsg_send_offchannel_raw(struct rpmsg_device *rpdev,
 		}
 	}
 
-	msg->len = len;
+	msg->len = cpu_to_virtio16(vrp->vdev, len);
 	msg->flags = 0;
-	msg->src = src;
-	msg->dst = dst;
+	msg->src = cpu_to_virtio32(vrp->vdev, src);
+	msg->dst = cpu_to_virtio32(vrp->vdev, dst);
 	msg->reserved = 0;
 	memcpy(msg->data, data, len);
 
 	dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
-		msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
+		src, dst, len, msg->flags, msg->reserved);
 #if defined(CONFIG_DYNAMIC_DEBUG)
 	dynamic_hex_dump("rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
 			 msg, sizeof(*msg) + msg->len, true);
@@ -704,13 +707,17 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
 {
 	struct rpmsg_endpoint *ept;
 	struct scatterlist sg;
+	unsigned int msg_len = virtio16_to_cpu(vrp->vdev, msg->len);
 	int err;
 
 	dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
-		msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
+		virtio32_to_cpu(vrp->vdev, msg->src),
+		virtio32_to_cpu(vrp->vdev, msg->dst), msg_len,
+		virtio16_to_cpu(vrp->vdev, msg->flags),
+		virtio32_to_cpu(vrp->vdev, msg->reserved));
 #if defined(CONFIG_DYNAMIC_DEBUG)
 	dynamic_hex_dump("rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
-			 msg, sizeof(*msg) + msg->len, true);
+			 msg, sizeof(*msg) + msg_len, true);
 #endif
 
 	/*
@@ -718,15 +725,15 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
 	 * the reported payload length.
 	 */
 	if (len > vrp->buf_size ||
-	    msg->len > (len - sizeof(struct rpmsg_hdr))) {
-		dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg->len);
+	    msg_len > (len - sizeof(struct rpmsg_hdr))) {
+		dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg_len);
 		return -EINVAL;
 	}
 
 	/* use the dst addr to fetch the callback of the appropriate user */
 	mutex_lock(&vrp->endpoints_lock);
 
-	ept = idr_find(&vrp->endpoints, msg->dst);
+	ept = idr_find(&vrp->endpoints, virtio32_to_cpu(vrp->vdev, msg->dst));
 
 	/* let's make sure no one deallocates ept while we use it */
 	if (ept)
@@ -739,8 +746,8 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
 		mutex_lock(&ept->cb_lock);
 
 		if (ept->cb)
-			ept->cb(ept->rpdev, msg->data, msg->len, ept->priv,
-				msg->src);
+			ept->cb(ept->rpdev, msg->data, msg_len, ept->priv,
+				virtio32_to_cpu(vrp->vdev, msg->src));
 
 		mutex_unlock(&ept->cb_lock);
 
@@ -846,15 +853,15 @@ static int rpmsg_ns_cb(struct rpmsg_device *rpdev, void *data, int len,
 	/* don't trust the remote processor for null terminating the name */
 	msg->name[RPMSG_NAME_SIZE - 1] = '\0';
 
-	dev_info(dev, "%sing channel %s addr 0x%x\n",
-		 msg->flags & RPMSG_NS_DESTROY ? "destroy" : "creat",
-		 msg->name, msg->addr);
-
 	strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
 	chinfo.src = RPMSG_ADDR_ANY;
-	chinfo.dst = msg->addr;
+	chinfo.dst = virtio32_to_cpu(vrp->vdev, msg->addr);
+
+	dev_info(dev, "%sing channel %s addr 0x%x\n",
+		 virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY ?
+		 "destroy" : "creat", msg->name, chinfo.dst);
 
-	if (msg->flags & RPMSG_NS_DESTROY) {
+	if (virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY) {
 		ret = rpmsg_unregister_device(&vrp->vdev->dev, &chinfo);
 		if (ret)
 			dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
-- 
1.9.3


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2] rpmsg: virtio: add endianness conversions
  2020-06-16  7:30     ` [PATCH v2] " Guennadi Liakhovetski
@ 2020-06-30 17:50       ` Mathieu Poirier
  2020-07-01 10:00         ` Michael S. Tsirkin
  0 siblings, 1 reply; 21+ messages in thread
From: Mathieu Poirier @ 2020-06-30 17:50 UTC (permalink / raw)
  To: Guennadi Liakhovetski
  Cc: linux-remoteproc, Michael S. Tsirkin, Ohad Ben-Cohen,
	Bjorn Andersson, Pierre-Louis Bossart, Liam Girdwood

On Tue, Jun 16, 2020 at 09:30:11AM +0200, Guennadi Liakhovetski wrote:
> According to the VirtIO 1.0 spec data, sent over virtual queues must
> be in little-endian format. Update the RPMsg VirtIO implementation
> to enforce that but let legacy configurations continue use native
> endianness.
> 
> Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
> ---
> 
> v2: Following suggestions from Michael and Mathieu switch to using virtio16/32 
> types and conversion functions.
> 
>  drivers/rpmsg/virtio_rpmsg_bus.c | 63 ++++++++++++++++++++++------------------
>  1 file changed, 35 insertions(+), 28 deletions(-)
> 
> diff --git a/drivers/rpmsg/virtio_rpmsg_bus.c b/drivers/rpmsg/virtio_rpmsg_bus.c
> index 07d4f33..41025df 100644
> --- a/drivers/rpmsg/virtio_rpmsg_bus.c
> +++ b/drivers/rpmsg/virtio_rpmsg_bus.c
> @@ -11,6 +11,8 @@
>  
>  #define pr_fmt(fmt) "%s: " fmt, __func__
>  
> +#include <asm/byteorder.h>
> +

Not sure about this header - what does it provide?

>  #include <linux/dma-mapping.h>
>  #include <linux/idr.h>
>  #include <linux/jiffies.h>
> @@ -22,6 +24,7 @@
>  #include <linux/scatterlist.h>
>  #include <linux/slab.h>
>  #include <linux/sched.h>
> +#include <linux/types.h>

This too... It should be fine with linux/virtio_config.h below since it 
already includes linux/virtio_byteorder.h

Moreover, please send another revision that applies on rproc-next.  The third
hunk in this patch doesn't apply.

With the above:

Reviewed-by: Mathieu Poirier <mathieu.poirier@linaro.org>

>  #include <linux/virtio.h>
>  #include <linux/virtio_ids.h>
>  #include <linux/virtio_config.h>
> @@ -84,11 +87,11 @@ struct virtproc_info {
>   * Every message sent(/received) on the rpmsg bus begins with this header.
>   */
>  struct rpmsg_hdr {
> -	u32 src;
> -	u32 dst;
> -	u32 reserved;
> -	u16 len;
> -	u16 flags;
> +	__virtio32 src;
> +	__virtio32 dst;
> +	__virtio32 reserved;
> +	__virtio16 len;
> +	__virtio16 flags;
>  	u8 data[];
>  } __packed;
>  
> @@ -106,8 +109,8 @@ struct rpmsg_hdr {
>   */
>  struct rpmsg_ns_msg {
>  	char name[RPMSG_NAME_SIZE];
> -	u32 addr;
> -	u32 flags;
> +	__virtio32 addr;
> +	__virtio32 flags;
>  } __packed;
>  
>  /**
> @@ -335,8 +338,8 @@ static int virtio_rpmsg_announce_create(struct rpmsg_device *rpdev)
>  		struct rpmsg_ns_msg nsm;
>  
>  		strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
> -		nsm.addr = rpdev->ept->addr;
> -		nsm.flags = RPMSG_NS_CREATE;
> +		nsm.addr = cpu_to_virtio32(vrp->vdev, rpdev->ept->addr);
> +		nsm.flags = cpu_to_virtio32(vrp->vdev, RPMSG_NS_CREATE);
>  
>  		err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
>  		if (err)
> @@ -359,8 +362,8 @@ static int virtio_rpmsg_announce_destroy(struct rpmsg_device *rpdev)
>  		struct rpmsg_ns_msg nsm;
>  
>  		strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
> -		nsm.addr = rpdev->ept->addr;
> -		nsm.flags = RPMSG_NS_DESTROY;
> +		nsm.addr = cpu_to_virtio32(vrp->vdev, rpdev->ept->addr);
> +		nsm.flags = cpu_to_virtio32(vrp->vdev, RPMSG_NS_DESTROY);
>  
>  		err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
>  		if (err)
> @@ -612,15 +615,15 @@ static int rpmsg_send_offchannel_raw(struct rpmsg_device *rpdev,
>  		}
>  	}
>  
> -	msg->len = len;
> +	msg->len = cpu_to_virtio16(vrp->vdev, len);
>  	msg->flags = 0;
> -	msg->src = src;
> -	msg->dst = dst;
> +	msg->src = cpu_to_virtio32(vrp->vdev, src);
> +	msg->dst = cpu_to_virtio32(vrp->vdev, dst);
>  	msg->reserved = 0;
>  	memcpy(msg->data, data, len);
>  
>  	dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
> -		msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
> +		src, dst, len, msg->flags, msg->reserved);
>  #if defined(CONFIG_DYNAMIC_DEBUG)
>  	dynamic_hex_dump("rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
>  			 msg, sizeof(*msg) + msg->len, true);
> @@ -704,13 +707,17 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
>  {
>  	struct rpmsg_endpoint *ept;
>  	struct scatterlist sg;
> +	unsigned int msg_len = virtio16_to_cpu(vrp->vdev, msg->len);
>  	int err;
>  
>  	dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
> -		msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
> +		virtio32_to_cpu(vrp->vdev, msg->src),
> +		virtio32_to_cpu(vrp->vdev, msg->dst), msg_len,
> +		virtio16_to_cpu(vrp->vdev, msg->flags),
> +		virtio32_to_cpu(vrp->vdev, msg->reserved));
>  #if defined(CONFIG_DYNAMIC_DEBUG)
>  	dynamic_hex_dump("rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
> -			 msg, sizeof(*msg) + msg->len, true);
> +			 msg, sizeof(*msg) + msg_len, true);
>  #endif
>  
>  	/*
> @@ -718,15 +725,15 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
>  	 * the reported payload length.
>  	 */
>  	if (len > vrp->buf_size ||
> -	    msg->len > (len - sizeof(struct rpmsg_hdr))) {
> -		dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg->len);
> +	    msg_len > (len - sizeof(struct rpmsg_hdr))) {
> +		dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg_len);
>  		return -EINVAL;
>  	}
>  
>  	/* use the dst addr to fetch the callback of the appropriate user */
>  	mutex_lock(&vrp->endpoints_lock);
>  
> -	ept = idr_find(&vrp->endpoints, msg->dst);
> +	ept = idr_find(&vrp->endpoints, virtio32_to_cpu(vrp->vdev, msg->dst));
>  
>  	/* let's make sure no one deallocates ept while we use it */
>  	if (ept)
> @@ -739,8 +746,8 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
>  		mutex_lock(&ept->cb_lock);
>  
>  		if (ept->cb)
> -			ept->cb(ept->rpdev, msg->data, msg->len, ept->priv,
> -				msg->src);
> +			ept->cb(ept->rpdev, msg->data, msg_len, ept->priv,
> +				virtio32_to_cpu(vrp->vdev, msg->src));
>  
>  		mutex_unlock(&ept->cb_lock);
>  
> @@ -846,15 +853,15 @@ static int rpmsg_ns_cb(struct rpmsg_device *rpdev, void *data, int len,
>  	/* don't trust the remote processor for null terminating the name */
>  	msg->name[RPMSG_NAME_SIZE - 1] = '\0';
>  
> -	dev_info(dev, "%sing channel %s addr 0x%x\n",
> -		 msg->flags & RPMSG_NS_DESTROY ? "destroy" : "creat",
> -		 msg->name, msg->addr);
> -
>  	strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
>  	chinfo.src = RPMSG_ADDR_ANY;
> -	chinfo.dst = msg->addr;
> +	chinfo.dst = virtio32_to_cpu(vrp->vdev, msg->addr);
> +
> +	dev_info(dev, "%sing channel %s addr 0x%x\n",
> +		 virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY ?
> +		 "destroy" : "creat", msg->name, chinfo.dst);
>  
> -	if (msg->flags & RPMSG_NS_DESTROY) {
> +	if (virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY) {
>  		ret = rpmsg_unregister_device(&vrp->vdev->dev, &chinfo);
>  		if (ret)
>  			dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
> -- 
> 1.9.3
> 

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2] rpmsg: virtio: add endianness conversions
  2020-06-30 17:50       ` Mathieu Poirier
@ 2020-07-01 10:00         ` Michael S. Tsirkin
  2020-07-06 12:47           ` Guennadi Liakhovetski
  0 siblings, 1 reply; 21+ messages in thread
From: Michael S. Tsirkin @ 2020-07-01 10:00 UTC (permalink / raw)
  To: Mathieu Poirier
  Cc: Guennadi Liakhovetski, linux-remoteproc, Ohad Ben-Cohen,
	Bjorn Andersson, Pierre-Louis Bossart, Liam Girdwood

On Tue, Jun 30, 2020 at 11:50:57AM -0600, Mathieu Poirier wrote:
> On Tue, Jun 16, 2020 at 09:30:11AM +0200, Guennadi Liakhovetski wrote:
> > According to the VirtIO 1.0 spec data, sent over virtual queues must
> > be in little-endian format. Update the RPMsg VirtIO implementation
> > to enforce that but let legacy configurations continue use native
> > endianness.
> > 
> > Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
> > ---
> > 
> > v2: Following suggestions from Michael and Mathieu switch to using virtio16/32 
> > types and conversion functions.
> > 
> >  drivers/rpmsg/virtio_rpmsg_bus.c | 63 ++++++++++++++++++++++------------------
> >  1 file changed, 35 insertions(+), 28 deletions(-)
> > 
> > diff --git a/drivers/rpmsg/virtio_rpmsg_bus.c b/drivers/rpmsg/virtio_rpmsg_bus.c
> > index 07d4f33..41025df 100644
> > --- a/drivers/rpmsg/virtio_rpmsg_bus.c
> > +++ b/drivers/rpmsg/virtio_rpmsg_bus.c
> > @@ -11,6 +11,8 @@
> >  
> >  #define pr_fmt(fmt) "%s: " fmt, __func__
> >  
> > +#include <asm/byteorder.h>
> > +
> 
> Not sure about this header - what does it provide?
> 
> >  #include <linux/dma-mapping.h>
> >  #include <linux/idr.h>
> >  #include <linux/jiffies.h>
> > @@ -22,6 +24,7 @@
> >  #include <linux/scatterlist.h>
> >  #include <linux/slab.h>
> >  #include <linux/sched.h>
> > +#include <linux/types.h>
> 
> This too... It should be fine with linux/virtio_config.h below since it 
> already includes linux/virtio_byteorder.h

It's probably best to include linux/virtio_byteorder.h explicitly.

> Moreover, please send another revision that applies on rproc-next.  The third
> hunk in this patch doesn't apply.
> 
> With the above:
> 
> Reviewed-by: Mathieu Poirier <mathieu.poirier@linaro.org>
> 
> >  #include <linux/virtio.h>
> >  #include <linux/virtio_ids.h>
> >  #include <linux/virtio_config.h>
> > @@ -84,11 +87,11 @@ struct virtproc_info {
> >   * Every message sent(/received) on the rpmsg bus begins with this header.
> >   */
> >  struct rpmsg_hdr {
> > -	u32 src;
> > -	u32 dst;
> > -	u32 reserved;
> > -	u16 len;
> > -	u16 flags;
> > +	__virtio32 src;
> > +	__virtio32 dst;
> > +	__virtio32 reserved;
> > +	__virtio16 len;
> > +	__virtio16 flags;
> >  	u8 data[];
> >  } __packed;
> >  
> > @@ -106,8 +109,8 @@ struct rpmsg_hdr {
> >   */
> >  struct rpmsg_ns_msg {
> >  	char name[RPMSG_NAME_SIZE];
> > -	u32 addr;
> > -	u32 flags;
> > +	__virtio32 addr;
> > +	__virtio32 flags;
> >  } __packed;
> >  
> >  /**
> > @@ -335,8 +338,8 @@ static int virtio_rpmsg_announce_create(struct rpmsg_device *rpdev)
> >  		struct rpmsg_ns_msg nsm;
> >  
> >  		strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
> > -		nsm.addr = rpdev->ept->addr;
> > -		nsm.flags = RPMSG_NS_CREATE;
> > +		nsm.addr = cpu_to_virtio32(vrp->vdev, rpdev->ept->addr);
> > +		nsm.flags = cpu_to_virtio32(vrp->vdev, RPMSG_NS_CREATE);
> >  
> >  		err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
> >  		if (err)
> > @@ -359,8 +362,8 @@ static int virtio_rpmsg_announce_destroy(struct rpmsg_device *rpdev)
> >  		struct rpmsg_ns_msg nsm;
> >  
> >  		strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
> > -		nsm.addr = rpdev->ept->addr;
> > -		nsm.flags = RPMSG_NS_DESTROY;
> > +		nsm.addr = cpu_to_virtio32(vrp->vdev, rpdev->ept->addr);
> > +		nsm.flags = cpu_to_virtio32(vrp->vdev, RPMSG_NS_DESTROY);
> >  
> >  		err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
> >  		if (err)
> > @@ -612,15 +615,15 @@ static int rpmsg_send_offchannel_raw(struct rpmsg_device *rpdev,
> >  		}
> >  	}
> >  
> > -	msg->len = len;
> > +	msg->len = cpu_to_virtio16(vrp->vdev, len);
> >  	msg->flags = 0;
> > -	msg->src = src;
> > -	msg->dst = dst;
> > +	msg->src = cpu_to_virtio32(vrp->vdev, src);
> > +	msg->dst = cpu_to_virtio32(vrp->vdev, dst);
> >  	msg->reserved = 0;
> >  	memcpy(msg->data, data, len);
> >  
> >  	dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
> > -		msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
> > +		src, dst, len, msg->flags, msg->reserved);
> >  #if defined(CONFIG_DYNAMIC_DEBUG)
> >  	dynamic_hex_dump("rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
> >  			 msg, sizeof(*msg) + msg->len, true);
> > @@ -704,13 +707,17 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
> >  {
> >  	struct rpmsg_endpoint *ept;
> >  	struct scatterlist sg;
> > +	unsigned int msg_len = virtio16_to_cpu(vrp->vdev, msg->len);
> >  	int err;
> >  
> >  	dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
> > -		msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
> > +		virtio32_to_cpu(vrp->vdev, msg->src),
> > +		virtio32_to_cpu(vrp->vdev, msg->dst), msg_len,
> > +		virtio16_to_cpu(vrp->vdev, msg->flags),
> > +		virtio32_to_cpu(vrp->vdev, msg->reserved));
> >  #if defined(CONFIG_DYNAMIC_DEBUG)
> >  	dynamic_hex_dump("rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
> > -			 msg, sizeof(*msg) + msg->len, true);
> > +			 msg, sizeof(*msg) + msg_len, true);
> >  #endif
> >  
> >  	/*
> > @@ -718,15 +725,15 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
> >  	 * the reported payload length.
> >  	 */
> >  	if (len > vrp->buf_size ||
> > -	    msg->len > (len - sizeof(struct rpmsg_hdr))) {
> > -		dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg->len);
> > +	    msg_len > (len - sizeof(struct rpmsg_hdr))) {
> > +		dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg_len);
> >  		return -EINVAL;
> >  	}
> >  
> >  	/* use the dst addr to fetch the callback of the appropriate user */
> >  	mutex_lock(&vrp->endpoints_lock);
> >  
> > -	ept = idr_find(&vrp->endpoints, msg->dst);
> > +	ept = idr_find(&vrp->endpoints, virtio32_to_cpu(vrp->vdev, msg->dst));
> >  
> >  	/* let's make sure no one deallocates ept while we use it */
> >  	if (ept)
> > @@ -739,8 +746,8 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
> >  		mutex_lock(&ept->cb_lock);
> >  
> >  		if (ept->cb)
> > -			ept->cb(ept->rpdev, msg->data, msg->len, ept->priv,
> > -				msg->src);
> > +			ept->cb(ept->rpdev, msg->data, msg_len, ept->priv,
> > +				virtio32_to_cpu(vrp->vdev, msg->src));
> >  
> >  		mutex_unlock(&ept->cb_lock);
> >  
> > @@ -846,15 +853,15 @@ static int rpmsg_ns_cb(struct rpmsg_device *rpdev, void *data, int len,
> >  	/* don't trust the remote processor for null terminating the name */
> >  	msg->name[RPMSG_NAME_SIZE - 1] = '\0';
> >  
> > -	dev_info(dev, "%sing channel %s addr 0x%x\n",
> > -		 msg->flags & RPMSG_NS_DESTROY ? "destroy" : "creat",
> > -		 msg->name, msg->addr);
> > -
> >  	strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
> >  	chinfo.src = RPMSG_ADDR_ANY;
> > -	chinfo.dst = msg->addr;
> > +	chinfo.dst = virtio32_to_cpu(vrp->vdev, msg->addr);
> > +
> > +	dev_info(dev, "%sing channel %s addr 0x%x\n",


Let's not try tricks. Just %s and destroying/creating below.
Wastes 3 bytes of kernel space but is clearer.

> > +		 virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY ?
> > +		 "destroy" : "creat", msg->name, chinfo.dst);
> >  
> > -	if (msg->flags & RPMSG_NS_DESTROY) {
> > +	if (virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY) {
> >  		ret = rpmsg_unregister_device(&vrp->vdev->dev, &chinfo);
> >  		if (ret)
> >  			dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
> > -- 
> > 1.9.3
> > 


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2] rpmsg: virtio: add endianness conversions
  2020-07-01 10:00         ` Michael S. Tsirkin
@ 2020-07-06 12:47           ` Guennadi Liakhovetski
  2020-07-06 12:56             ` Guennadi Liakhovetski
  0 siblings, 1 reply; 21+ messages in thread
From: Guennadi Liakhovetski @ 2020-07-06 12:47 UTC (permalink / raw)
  To: Michael S. Tsirkin
  Cc: Mathieu Poirier, linux-remoteproc, Ohad Ben-Cohen,
	Bjorn Andersson, Pierre-Louis Bossart, Liam Girdwood

On Wed, Jul 01, 2020 at 06:00:30AM -0400, Michael S. Tsirkin wrote:
> On Tue, Jun 30, 2020 at 11:50:57AM -0600, Mathieu Poirier wrote:
> > On Tue, Jun 16, 2020 at 09:30:11AM +0200, Guennadi Liakhovetski wrote:
> > > According to the VirtIO 1.0 spec data, sent over virtual queues must
> > > be in little-endian format. Update the RPMsg VirtIO implementation
> > > to enforce that but let legacy configurations continue use native
> > > endianness.
> > > 
> > > Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
> > > ---
> > > 
> > > v2: Following suggestions from Michael and Mathieu switch to using virtio16/32 
> > > types and conversion functions.
> > > 
> > >  drivers/rpmsg/virtio_rpmsg_bus.c | 63 ++++++++++++++++++++++------------------
> > >  1 file changed, 35 insertions(+), 28 deletions(-)
> > > 
> > > diff --git a/drivers/rpmsg/virtio_rpmsg_bus.c b/drivers/rpmsg/virtio_rpmsg_bus.c
> > > index 07d4f33..41025df 100644
> > > --- a/drivers/rpmsg/virtio_rpmsg_bus.c
> > > +++ b/drivers/rpmsg/virtio_rpmsg_bus.c
> > > @@ -11,6 +11,8 @@
> > >  
> > >  #define pr_fmt(fmt) "%s: " fmt, __func__
> > >  
> > > +#include <asm/byteorder.h>
> > > +
> > 
> > Not sure about this header - what does it provide?

It was needed for the original cpu_to_le... and friends version of the patch.

> > 
> > >  #include <linux/dma-mapping.h>
> > >  #include <linux/idr.h>
> > >  #include <linux/jiffies.h>
> > > @@ -22,6 +24,7 @@
> > >  #include <linux/scatterlist.h>
> > >  #include <linux/slab.h>
> > >  #include <linux/sched.h>
> > > +#include <linux/types.h>
> > 
> > This too... It should be fine with linux/virtio_config.h below since it 
> > already includes linux/virtio_byteorder.h
> 
> It's probably best to include linux/virtio_byteorder.h explicitly.

Yep, will do.

> > Moreover, please send another revision that applies on rproc-next.  The third
> > hunk in this patch doesn't apply.
> > 
> > With the above:
> > 
> > Reviewed-by: Mathieu Poirier <mathieu.poirier@linaro.org>
> > 
> > >  #include <linux/virtio.h>
> > >  #include <linux/virtio_ids.h>
> > >  #include <linux/virtio_config.h>

[snip]

> > > @@ -846,15 +853,15 @@ static int rpmsg_ns_cb(struct rpmsg_device *rpdev, void *data, int len,
> > >  	/* don't trust the remote processor for null terminating the name */
> > >  	msg->name[RPMSG_NAME_SIZE - 1] = '\0';
> > >  
> > > -	dev_info(dev, "%sing channel %s addr 0x%x\n",
> > > -		 msg->flags & RPMSG_NS_DESTROY ? "destroy" : "creat",
> > > -		 msg->name, msg->addr);
> > > -
> > >  	strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
> > >  	chinfo.src = RPMSG_ADDR_ANY;
> > > -	chinfo.dst = msg->addr;
> > > +	chinfo.dst = virtio32_to_cpu(vrp->vdev, msg->addr);
> > > +
> > > +	dev_info(dev, "%sing channel %s addr 0x%x\n",
> 
> 
> Let's not try tricks. Just %s and destroying/creating below.
> Wastes 3 bytes of kernel space but is clearer.

Those aren't my tricks - they're already there, but sure, I can replace that while at it.

Thanks
Guennadi

> 
> > > +		 virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY ?
> > > +		 "destroy" : "creat", msg->name, chinfo.dst);
> > >  
> > > -	if (msg->flags & RPMSG_NS_DESTROY) {
> > > +	if (virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY) {
> > >  		ret = rpmsg_unregister_device(&vrp->vdev->dev, &chinfo);
> > >  		if (ret)
> > >  			dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
> > > -- 
> > > 1.9.3
> > > 
> 

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2] rpmsg: virtio: add endianness conversions
  2020-07-06 12:47           ` Guennadi Liakhovetski
@ 2020-07-06 12:56             ` Guennadi Liakhovetski
  2020-07-06 13:38               ` [PATCH v3] " Guennadi Liakhovetski
  0 siblings, 1 reply; 21+ messages in thread
From: Guennadi Liakhovetski @ 2020-07-06 12:56 UTC (permalink / raw)
  To: Michael S. Tsirkin
  Cc: Mathieu Poirier, linux-remoteproc, Ohad Ben-Cohen,
	Bjorn Andersson, Pierre-Louis Bossart, Liam Girdwood

On Mon, Jul 06, 2020 at 02:47:16PM +0200, Guennadi Liakhovetski wrote:
> On Wed, Jul 01, 2020 at 06:00:30AM -0400, Michael S. Tsirkin wrote:
> > On Tue, Jun 30, 2020 at 11:50:57AM -0600, Mathieu Poirier wrote:
> > > On Tue, Jun 16, 2020 at 09:30:11AM +0200, Guennadi Liakhovetski wrote:
> > > > According to the VirtIO 1.0 spec data, sent over virtual queues must
> > > > be in little-endian format. Update the RPMsg VirtIO implementation
> > > > to enforce that but let legacy configurations continue use native
> > > > endianness.
> > > > 
> > > > Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
> > > > ---
> > > > 
> > > > v2: Following suggestions from Michael and Mathieu switch to using virtio16/32 
> > > > types and conversion functions.
> > > > 
> > > >  drivers/rpmsg/virtio_rpmsg_bus.c | 63 ++++++++++++++++++++++------------------
> > > >  1 file changed, 35 insertions(+), 28 deletions(-)
> > > > 
> > > > diff --git a/drivers/rpmsg/virtio_rpmsg_bus.c b/drivers/rpmsg/virtio_rpmsg_bus.c
> > > > index 07d4f33..41025df 100644
> > > > --- a/drivers/rpmsg/virtio_rpmsg_bus.c
> > > > +++ b/drivers/rpmsg/virtio_rpmsg_bus.c

[snip]

> > > > @@ -846,15 +853,15 @@ static int rpmsg_ns_cb(struct rpmsg_device *rpdev, void *data, int len,
> > > >  	/* don't trust the remote processor for null terminating the name */
> > > >  	msg->name[RPMSG_NAME_SIZE - 1] = '\0';
> > > >  
> > > > -	dev_info(dev, "%sing channel %s addr 0x%x\n",
> > > > -		 msg->flags & RPMSG_NS_DESTROY ? "destroy" : "creat",
> > > > -		 msg->name, msg->addr);
> > > > -
> > > >  	strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
> > > >  	chinfo.src = RPMSG_ADDR_ANY;
> > > > -	chinfo.dst = msg->addr;
> > > > +	chinfo.dst = virtio32_to_cpu(vrp->vdev, msg->addr);
> > > > +
> > > > +	dev_info(dev, "%sing channel %s addr 0x%x\n",
> > 
> > 
> > Let's not try tricks. Just %s and destroying/creating below.
> > Wastes 3 bytes of kernel space but is clearer.
> 
> Those aren't my tricks - they're already there, but sure, I can replace that while at it.

On a second thought, that would be an unrelated change, so, I should probably keep it 
as is.

Thanks
Guennadi

> > > > +		 virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY ?
> > > > +		 "destroy" : "creat", msg->name, chinfo.dst);
> > > >  
> > > > -	if (msg->flags & RPMSG_NS_DESTROY) {
> > > > +	if (virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY) {
> > > >  		ret = rpmsg_unregister_device(&vrp->vdev->dev, &chinfo);
> > > >  		if (ret)
> > > >  			dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
> > > > -- 
> > > > 1.9.3

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [PATCH v3] rpmsg: virtio: add endianness conversions
  2020-07-06 12:56             ` Guennadi Liakhovetski
@ 2020-07-06 13:38               ` Guennadi Liakhovetski
  2020-07-07 15:11                 ` Arnaud POULIQUEN
  0 siblings, 1 reply; 21+ messages in thread
From: Guennadi Liakhovetski @ 2020-07-06 13:38 UTC (permalink / raw)
  To: Michael S. Tsirkin
  Cc: Mathieu Poirier, linux-remoteproc, Ohad Ben-Cohen,
	Bjorn Andersson, Pierre-Louis Bossart, Liam Girdwood

According to the VirtIO 1.0 spec data, sent over virtual queues must
be in little-endian format. Update the RPMsg VirtIO implementation
to enforce that but let legacy configurations continue use native
endianness.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
Reviewed-by: Mathieu Poirier <mathieu.poirier@linaro.org>
---

v3: use the virtio_byteorder.h header

v2: Following suggestions from Michael and Mathieu switch to using virtio16/32 
types and conversion functions.

 drivers/rpmsg/virtio_rpmsg_bus.c | 61 +++++++++++++++++---------------
 1 file changed, 33 insertions(+), 28 deletions(-)

diff --git a/drivers/rpmsg/virtio_rpmsg_bus.c b/drivers/rpmsg/virtio_rpmsg_bus.c
index 07d4f3374098..4262170ca093 100644
--- a/drivers/rpmsg/virtio_rpmsg_bus.c
+++ b/drivers/rpmsg/virtio_rpmsg_bus.c
@@ -23,6 +23,7 @@
 #include <linux/slab.h>
 #include <linux/sched.h>
 #include <linux/virtio.h>
+#include <linux/virtio_byteorder.h>
 #include <linux/virtio_ids.h>
 #include <linux/virtio_config.h>
 #include <linux/wait.h>
@@ -84,11 +85,11 @@ struct virtproc_info {
  * Every message sent(/received) on the rpmsg bus begins with this header.
  */
 struct rpmsg_hdr {
-	u32 src;
-	u32 dst;
-	u32 reserved;
-	u16 len;
-	u16 flags;
+	__virtio32 src;
+	__virtio32 dst;
+	__virtio32 reserved;
+	__virtio16 len;
+	__virtio16 flags;
 	u8 data[];
 } __packed;
 
@@ -106,8 +107,8 @@ struct rpmsg_hdr {
  */
 struct rpmsg_ns_msg {
 	char name[RPMSG_NAME_SIZE];
-	u32 addr;
-	u32 flags;
+	__virtio32 addr;
+	__virtio32 flags;
 } __packed;
 
 /**
@@ -335,8 +336,8 @@ static int virtio_rpmsg_announce_create(struct rpmsg_device *rpdev)
 		struct rpmsg_ns_msg nsm;
 
 		strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
-		nsm.addr = rpdev->ept->addr;
-		nsm.flags = RPMSG_NS_CREATE;
+		nsm.addr = cpu_to_virtio32(vrp->vdev, rpdev->ept->addr);
+		nsm.flags = cpu_to_virtio32(vrp->vdev, RPMSG_NS_CREATE);
 
 		err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
 		if (err)
@@ -359,8 +360,8 @@ static int virtio_rpmsg_announce_destroy(struct rpmsg_device *rpdev)
 		struct rpmsg_ns_msg nsm;
 
 		strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
-		nsm.addr = rpdev->ept->addr;
-		nsm.flags = RPMSG_NS_DESTROY;
+		nsm.addr = cpu_to_virtio32(vrp->vdev, rpdev->ept->addr);
+		nsm.flags = cpu_to_virtio32(vrp->vdev, RPMSG_NS_DESTROY);
 
 		err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
 		if (err)
@@ -612,15 +613,15 @@ static int rpmsg_send_offchannel_raw(struct rpmsg_device *rpdev,
 		}
 	}
 
-	msg->len = len;
+	msg->len = cpu_to_virtio16(vrp->vdev, len);
 	msg->flags = 0;
-	msg->src = src;
-	msg->dst = dst;
+	msg->src = cpu_to_virtio32(vrp->vdev, src);
+	msg->dst = cpu_to_virtio32(vrp->vdev, dst);
 	msg->reserved = 0;
 	memcpy(msg->data, data, len);
 
 	dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
-		msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
+		src, dst, len, msg->flags, msg->reserved);
 #if defined(CONFIG_DYNAMIC_DEBUG)
 	dynamic_hex_dump("rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
 			 msg, sizeof(*msg) + msg->len, true);
@@ -704,13 +705,17 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
 {
 	struct rpmsg_endpoint *ept;
 	struct scatterlist sg;
+	unsigned int msg_len = virtio16_to_cpu(vrp->vdev, msg->len);
 	int err;
 
 	dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
-		msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
+		virtio32_to_cpu(vrp->vdev, msg->src),
+		virtio32_to_cpu(vrp->vdev, msg->dst), msg_len,
+		virtio16_to_cpu(vrp->vdev, msg->flags),
+		virtio32_to_cpu(vrp->vdev, msg->reserved));
 #if defined(CONFIG_DYNAMIC_DEBUG)
 	dynamic_hex_dump("rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
-			 msg, sizeof(*msg) + msg->len, true);
+			 msg, sizeof(*msg) + msg_len, true);
 #endif
 
 	/*
@@ -718,15 +723,15 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
 	 * the reported payload length.
 	 */
 	if (len > vrp->buf_size ||
-	    msg->len > (len - sizeof(struct rpmsg_hdr))) {
-		dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg->len);
+	    msg_len > (len - sizeof(struct rpmsg_hdr))) {
+		dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg_len);
 		return -EINVAL;
 	}
 
 	/* use the dst addr to fetch the callback of the appropriate user */
 	mutex_lock(&vrp->endpoints_lock);
 
-	ept = idr_find(&vrp->endpoints, msg->dst);
+	ept = idr_find(&vrp->endpoints, virtio32_to_cpu(vrp->vdev, msg->dst));
 
 	/* let's make sure no one deallocates ept while we use it */
 	if (ept)
@@ -739,8 +744,8 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
 		mutex_lock(&ept->cb_lock);
 
 		if (ept->cb)
-			ept->cb(ept->rpdev, msg->data, msg->len, ept->priv,
-				msg->src);
+			ept->cb(ept->rpdev, msg->data, msg_len, ept->priv,
+				virtio32_to_cpu(vrp->vdev, msg->src));
 
 		mutex_unlock(&ept->cb_lock);
 
@@ -846,15 +851,15 @@ static int rpmsg_ns_cb(struct rpmsg_device *rpdev, void *data, int len,
 	/* don't trust the remote processor for null terminating the name */
 	msg->name[RPMSG_NAME_SIZE - 1] = '\0';
 
-	dev_info(dev, "%sing channel %s addr 0x%x\n",
-		 msg->flags & RPMSG_NS_DESTROY ? "destroy" : "creat",
-		 msg->name, msg->addr);
-
 	strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
 	chinfo.src = RPMSG_ADDR_ANY;
-	chinfo.dst = msg->addr;
+	chinfo.dst = virtio32_to_cpu(vrp->vdev, msg->addr);
+
+	dev_info(dev, "%sing channel %s addr 0x%x\n",
+		 virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY ?
+		 "destroy" : "creat", msg->name, chinfo.dst);
 
-	if (msg->flags & RPMSG_NS_DESTROY) {
+	if (virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY) {
 		ret = rpmsg_unregister_device(&vrp->vdev->dev, &chinfo);
 		if (ret)
 			dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
-- 
2.27.0


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v3] rpmsg: virtio: add endianness conversions
  2020-07-06 13:38               ` [PATCH v3] " Guennadi Liakhovetski
@ 2020-07-07 15:11                 ` Arnaud POULIQUEN
  2020-07-07 15:34                   ` [PATCH v4] " Guennadi Liakhovetski
  0 siblings, 1 reply; 21+ messages in thread
From: Arnaud POULIQUEN @ 2020-07-07 15:11 UTC (permalink / raw)
  To: Guennadi Liakhovetski, Michael S. Tsirkin
  Cc: Mathieu Poirier, linux-remoteproc, Ohad Ben-Cohen,
	Bjorn Andersson, Pierre-Louis Bossart, Liam Girdwood

Hi,

On 7/6/20 3:38 PM, Guennadi Liakhovetski wrote:
> According to the VirtIO 1.0 spec data, sent over virtual queues must
> be in little-endian format. Update the RPMsg VirtIO implementation
> to enforce that but let legacy configurations continue use native
> endianness.
> 
> Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
> Reviewed-by: Mathieu Poirier <mathieu.poirier@linaro.org>
> ---

As requested by Mathieu, I performed few tests on the stm32mp1 platform (little Endian), LGTM
No platform in ST to test the BE configuration.

I have just have a comment below then
 
Tested-by: Arnaud Pouliquen<arnaud.pouliquen@st.com>
 
> 
> v3: use the virtio_byteorder.h header
> 
> v2: Following suggestions from Michael and Mathieu switch to using virtio16/32 
> types and conversion functions.
> 
>  drivers/rpmsg/virtio_rpmsg_bus.c | 61 +++++++++++++++++---------------
>  1 file changed, 33 insertions(+), 28 deletions(-)
> 
> diff --git a/drivers/rpmsg/virtio_rpmsg_bus.c b/drivers/rpmsg/virtio_rpmsg_bus.c
> index 07d4f3374098..4262170ca093 100644
> --- a/drivers/rpmsg/virtio_rpmsg_bus.c
> +++ b/drivers/rpmsg/virtio_rpmsg_bus.c
> @@ -23,6 +23,7 @@
>  #include <linux/slab.h>
>  #include <linux/sched.h>
>  #include <linux/virtio.h>
> +#include <linux/virtio_byteorder.h>
>  #include <linux/virtio_ids.h>
>  #include <linux/virtio_config.h>
>  #include <linux/wait.h>
> @@ -84,11 +85,11 @@ struct virtproc_info {
>   * Every message sent(/received) on the rpmsg bus begins with this header.
>   */
>  struct rpmsg_hdr {
> -	u32 src;
> -	u32 dst;
> -	u32 reserved;
> -	u16 len;
> -	u16 flags;
> +	__virtio32 src;
> +	__virtio32 dst;
> +	__virtio32 reserved;
> +	__virtio16 len;
> +	__virtio16 flags;
>  	u8 data[];
>  } __packed;
>  
> @@ -106,8 +107,8 @@ struct rpmsg_hdr {
>   */
>  struct rpmsg_ns_msg {
>  	char name[RPMSG_NAME_SIZE];
> -	u32 addr;
> -	u32 flags;
> +	__virtio32 addr;
> +	__virtio32 flags;
>  } __packed;
>  
>  /**
> @@ -335,8 +336,8 @@ static int virtio_rpmsg_announce_create(struct rpmsg_device *rpdev)
>  		struct rpmsg_ns_msg nsm;
>  
>  		strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
> -		nsm.addr = rpdev->ept->addr;
> -		nsm.flags = RPMSG_NS_CREATE;
> +		nsm.addr = cpu_to_virtio32(vrp->vdev, rpdev->ept->addr);
> +		nsm.flags = cpu_to_virtio32(vrp->vdev, RPMSG_NS_CREATE);
>  
>  		err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
>  		if (err)
> @@ -359,8 +360,8 @@ static int virtio_rpmsg_announce_destroy(struct rpmsg_device *rpdev)
>  		struct rpmsg_ns_msg nsm;
>  
>  		strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
> -		nsm.addr = rpdev->ept->addr;
> -		nsm.flags = RPMSG_NS_DESTROY;
> +		nsm.addr = cpu_to_virtio32(vrp->vdev, rpdev->ept->addr);
> +		nsm.flags = cpu_to_virtio32(vrp->vdev, RPMSG_NS_DESTROY);
>  
>  		err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
>  		if (err)
> @@ -612,15 +613,15 @@ static int rpmsg_send_offchannel_raw(struct rpmsg_device *rpdev,
>  		}
>  	}
>  
> -	msg->len = len;
> +	msg->len = cpu_to_virtio16(vrp->vdev, len);
>  	msg->flags = 0;
> -	msg->src = src;
> -	msg->dst = dst;
> +	msg->src = cpu_to_virtio32(vrp->vdev, src);
> +	msg->dst = cpu_to_virtio32(vrp->vdev, dst);
>  	msg->reserved = 0;
>  	memcpy(msg->data, data, len);
>  
>  	dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
> -		msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
> +		src, dst, len, msg->flags, msg->reserved);
>  #if defined(CONFIG_DYNAMIC_DEBUG)
>  	dynamic_hex_dump("rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
>  			 msg, sizeof(*msg) + msg->len, true);

msg->len format is __virtio16, should be be replaced by len which respects CPU endianness.

Regards,
Arnaud

> @@ -704,13 +705,17 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
>  {
>  	struct rpmsg_endpoint *ept;
>  	struct scatterlist sg;
> +	unsigned int msg_len = virtio16_to_cpu(vrp->vdev, msg->len);
>  	int err;
>  
>  	dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
> -		msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
> +		virtio32_to_cpu(vrp->vdev, msg->src),
> +		virtio32_to_cpu(vrp->vdev, msg->dst), msg_len,
> +		virtio16_to_cpu(vrp->vdev, msg->flags),
> +		virtio32_to_cpu(vrp->vdev, msg->reserved));
>  #if defined(CONFIG_DYNAMIC_DEBUG)
>  	dynamic_hex_dump("rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
> -			 msg, sizeof(*msg) + msg->len, true);
> +			 msg, sizeof(*msg) + msg_len, true);
>  #endif
>  
>  	/*
> @@ -718,15 +723,15 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
>  	 * the reported payload length.
>  	 */
>  	if (len > vrp->buf_size ||
> -	    msg->len > (len - sizeof(struct rpmsg_hdr))) {
> -		dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg->len);
> +	    msg_len > (len - sizeof(struct rpmsg_hdr))) {
> +		dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg_len);
>  		return -EINVAL;
>  	}
>  
>  	/* use the dst addr to fetch the callback of the appropriate user */
>  	mutex_lock(&vrp->endpoints_lock);
>  
> -	ept = idr_find(&vrp->endpoints, msg->dst);
> +	ept = idr_find(&vrp->endpoints, virtio32_to_cpu(vrp->vdev, msg->dst));
>  
>  	/* let's make sure no one deallocates ept while we use it */
>  	if (ept)
> @@ -739,8 +744,8 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
>  		mutex_lock(&ept->cb_lock);
>  
>  		if (ept->cb)
> -			ept->cb(ept->rpdev, msg->data, msg->len, ept->priv,
> -				msg->src);
> +			ept->cb(ept->rpdev, msg->data, msg_len, ept->priv,
> +				virtio32_to_cpu(vrp->vdev, msg->src));
>  
>  		mutex_unlock(&ept->cb_lock);
>  
> @@ -846,15 +851,15 @@ static int rpmsg_ns_cb(struct rpmsg_device *rpdev, void *data, int len,
>  	/* don't trust the remote processor for null terminating the name */
>  	msg->name[RPMSG_NAME_SIZE - 1] = '\0';
>  
> -	dev_info(dev, "%sing channel %s addr 0x%x\n",
> -		 msg->flags & RPMSG_NS_DESTROY ? "destroy" : "creat",
> -		 msg->name, msg->addr);
> -
>  	strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
>  	chinfo.src = RPMSG_ADDR_ANY;
> -	chinfo.dst = msg->addr;
> +	chinfo.dst = virtio32_to_cpu(vrp->vdev, msg->addr);
> +
> +	dev_info(dev, "%sing channel %s addr 0x%x\n",
> +		 virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY ?
> +		 "destroy" : "creat", msg->name, chinfo.dst);
>  
> -	if (msg->flags & RPMSG_NS_DESTROY) {
> +	if (virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY) {
>  		ret = rpmsg_unregister_device(&vrp->vdev->dev, &chinfo);
>  		if (ret)
>  			dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
> 


^ permalink raw reply	[flat|nested] 21+ messages in thread

* [PATCH v4] rpmsg: virtio: add endianness conversions
  2020-07-07 15:11                 ` Arnaud POULIQUEN
@ 2020-07-07 15:34                   ` Guennadi Liakhovetski
  2020-07-07 15:38                     ` Mathieu Poirier
  0 siblings, 1 reply; 21+ messages in thread
From: Guennadi Liakhovetski @ 2020-07-07 15:34 UTC (permalink / raw)
  To: Arnaud POULIQUEN
  Cc: Michael S. Tsirkin, Mathieu Poirier, linux-remoteproc,
	Ohad Ben-Cohen, Bjorn Andersson, Pierre-Louis Bossart,
	Liam Girdwood

According to the VirtIO 1.0 spec data, sent over virtual queues must
be in little-endian format. Update the RPMsg VirtIO implementation
to enforce that but let legacy configurations continue use native
endianness.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
Tested-by: Arnaud Pouliquen <arnaud.pouliquen@st.com>
---

Mathieu, I had to remove your "Reviewed-by" because this is a modified version...

v4: fix a left-over use of an LE value in calculation - thanks to Arnaud for catching!

v3: use the virtio_byteorder.h header

v2: Following suggestions from Michael and Mathieu switch to using virtio16/32
types and conversion functions.

 drivers/rpmsg/virtio_rpmsg_bus.c | 63 +++++++++++++++++---------------
 1 file changed, 34 insertions(+), 29 deletions(-)

diff --git a/drivers/rpmsg/virtio_rpmsg_bus.c b/drivers/rpmsg/virtio_rpmsg_bus.c
index 07d4f3374098..9006fc7f73d0 100644
--- a/drivers/rpmsg/virtio_rpmsg_bus.c
+++ b/drivers/rpmsg/virtio_rpmsg_bus.c
@@ -23,6 +23,7 @@
 #include <linux/slab.h>
 #include <linux/sched.h>
 #include <linux/virtio.h>
+#include <linux/virtio_byteorder.h>
 #include <linux/virtio_ids.h>
 #include <linux/virtio_config.h>
 #include <linux/wait.h>
@@ -84,11 +85,11 @@ struct virtproc_info {
  * Every message sent(/received) on the rpmsg bus begins with this header.
  */
 struct rpmsg_hdr {
-	u32 src;
-	u32 dst;
-	u32 reserved;
-	u16 len;
-	u16 flags;
+	__virtio32 src;
+	__virtio32 dst;
+	__virtio32 reserved;
+	__virtio16 len;
+	__virtio16 flags;
 	u8 data[];
 } __packed;
 
@@ -106,8 +107,8 @@ struct rpmsg_hdr {
  */
 struct rpmsg_ns_msg {
 	char name[RPMSG_NAME_SIZE];
-	u32 addr;
-	u32 flags;
+	__virtio32 addr;
+	__virtio32 flags;
 } __packed;
 
 /**
@@ -335,8 +336,8 @@ static int virtio_rpmsg_announce_create(struct rpmsg_device *rpdev)
 		struct rpmsg_ns_msg nsm;
 
 		strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
-		nsm.addr = rpdev->ept->addr;
-		nsm.flags = RPMSG_NS_CREATE;
+		nsm.addr = cpu_to_virtio32(vrp->vdev, rpdev->ept->addr);
+		nsm.flags = cpu_to_virtio32(vrp->vdev, RPMSG_NS_CREATE);
 
 		err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
 		if (err)
@@ -359,8 +360,8 @@ static int virtio_rpmsg_announce_destroy(struct rpmsg_device *rpdev)
 		struct rpmsg_ns_msg nsm;
 
 		strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
-		nsm.addr = rpdev->ept->addr;
-		nsm.flags = RPMSG_NS_DESTROY;
+		nsm.addr = cpu_to_virtio32(vrp->vdev, rpdev->ept->addr);
+		nsm.flags = cpu_to_virtio32(vrp->vdev, RPMSG_NS_DESTROY);
 
 		err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
 		if (err)
@@ -612,18 +613,18 @@ static int rpmsg_send_offchannel_raw(struct rpmsg_device *rpdev,
 		}
 	}
 
-	msg->len = len;
+	msg->len = cpu_to_virtio16(vrp->vdev, len);
 	msg->flags = 0;
-	msg->src = src;
-	msg->dst = dst;
+	msg->src = cpu_to_virtio32(vrp->vdev, src);
+	msg->dst = cpu_to_virtio32(vrp->vdev, dst);
 	msg->reserved = 0;
 	memcpy(msg->data, data, len);
 
 	dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
-		msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
+		src, dst, len, msg->flags, msg->reserved);
 #if defined(CONFIG_DYNAMIC_DEBUG)
 	dynamic_hex_dump("rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
-			 msg, sizeof(*msg) + msg->len, true);
+			 msg, sizeof(*msg) + len, true);
 #endif
 
 	rpmsg_sg_init(&sg, msg, sizeof(*msg) + len);
@@ -704,13 +705,17 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
 {
 	struct rpmsg_endpoint *ept;
 	struct scatterlist sg;
+	unsigned int msg_len = virtio16_to_cpu(vrp->vdev, msg->len);
 	int err;
 
 	dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
-		msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
+		virtio32_to_cpu(vrp->vdev, msg->src),
+		virtio32_to_cpu(vrp->vdev, msg->dst), msg_len,
+		virtio16_to_cpu(vrp->vdev, msg->flags),
+		virtio32_to_cpu(vrp->vdev, msg->reserved));
 #if defined(CONFIG_DYNAMIC_DEBUG)
 	dynamic_hex_dump("rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
-			 msg, sizeof(*msg) + msg->len, true);
+			 msg, sizeof(*msg) + msg_len, true);
 #endif
 
 	/*
@@ -718,15 +723,15 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
 	 * the reported payload length.
 	 */
 	if (len > vrp->buf_size ||
-	    msg->len > (len - sizeof(struct rpmsg_hdr))) {
-		dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg->len);
+	    msg_len > (len - sizeof(struct rpmsg_hdr))) {
+		dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg_len);
 		return -EINVAL;
 	}
 
 	/* use the dst addr to fetch the callback of the appropriate user */
 	mutex_lock(&vrp->endpoints_lock);
 
-	ept = idr_find(&vrp->endpoints, msg->dst);
+	ept = idr_find(&vrp->endpoints, virtio32_to_cpu(vrp->vdev, msg->dst));
 
 	/* let's make sure no one deallocates ept while we use it */
 	if (ept)
@@ -739,8 +744,8 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
 		mutex_lock(&ept->cb_lock);
 
 		if (ept->cb)
-			ept->cb(ept->rpdev, msg->data, msg->len, ept->priv,
-				msg->src);
+			ept->cb(ept->rpdev, msg->data, msg_len, ept->priv,
+				virtio32_to_cpu(vrp->vdev, msg->src));
 
 		mutex_unlock(&ept->cb_lock);
 
@@ -846,15 +851,15 @@ static int rpmsg_ns_cb(struct rpmsg_device *rpdev, void *data, int len,
 	/* don't trust the remote processor for null terminating the name */
 	msg->name[RPMSG_NAME_SIZE - 1] = '\0';
 
-	dev_info(dev, "%sing channel %s addr 0x%x\n",
-		 msg->flags & RPMSG_NS_DESTROY ? "destroy" : "creat",
-		 msg->name, msg->addr);
-
 	strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
 	chinfo.src = RPMSG_ADDR_ANY;
-	chinfo.dst = msg->addr;
+	chinfo.dst = virtio32_to_cpu(vrp->vdev, msg->addr);
+
+	dev_info(dev, "%sing channel %s addr 0x%x\n",
+		 virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY ?
+		 "destroy" : "creat", msg->name, chinfo.dst);
 
-	if (msg->flags & RPMSG_NS_DESTROY) {
+	if (virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY) {
 		ret = rpmsg_unregister_device(&vrp->vdev->dev, &chinfo);
 		if (ret)
 			dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
-- 
2.27.0


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v4] rpmsg: virtio: add endianness conversions
  2020-07-07 15:34                   ` [PATCH v4] " Guennadi Liakhovetski
@ 2020-07-07 15:38                     ` Mathieu Poirier
  2020-07-10  6:20                       ` Guennadi Liakhovetski
  2020-07-21  8:56                       ` [PATCH RESEND " Guennadi Liakhovetski
  0 siblings, 2 replies; 21+ messages in thread
From: Mathieu Poirier @ 2020-07-07 15:38 UTC (permalink / raw)
  To: Guennadi Liakhovetski
  Cc: Arnaud POULIQUEN, Michael S. Tsirkin, linux-remoteproc,
	Ohad Ben-Cohen, Bjorn Andersson, Pierre-Louis Bossart,
	Liam Girdwood

On Tue, 7 Jul 2020 at 09:34, Guennadi Liakhovetski
<guennadi.liakhovetski@linux.intel.com> wrote:
>
> According to the VirtIO 1.0 spec data, sent over virtual queues must
> be in little-endian format. Update the RPMsg VirtIO implementation
> to enforce that but let legacy configurations continue use native
> endianness.
>
> Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
> Tested-by: Arnaud Pouliquen <arnaud.pouliquen@st.com>
> ---
>
> Mathieu, I had to remove your "Reviewed-by" because this is a modified version...
>
> v4: fix a left-over use of an LE value in calculation - thanks to Arnaud for catching!

It is indeed a good catch

Reviewed-by: Mathieu Poirier <mathieu.poirier@linaro.org>

>
> v3: use the virtio_byteorder.h header
>
> v2: Following suggestions from Michael and Mathieu switch to using virtio16/32
> types and conversion functions.
>
>  drivers/rpmsg/virtio_rpmsg_bus.c | 63 +++++++++++++++++---------------
>  1 file changed, 34 insertions(+), 29 deletions(-)
>
> diff --git a/drivers/rpmsg/virtio_rpmsg_bus.c b/drivers/rpmsg/virtio_rpmsg_bus.c
> index 07d4f3374098..9006fc7f73d0 100644
> --- a/drivers/rpmsg/virtio_rpmsg_bus.c
> +++ b/drivers/rpmsg/virtio_rpmsg_bus.c
> @@ -23,6 +23,7 @@
>  #include <linux/slab.h>
>  #include <linux/sched.h>
>  #include <linux/virtio.h>
> +#include <linux/virtio_byteorder.h>
>  #include <linux/virtio_ids.h>
>  #include <linux/virtio_config.h>
>  #include <linux/wait.h>
> @@ -84,11 +85,11 @@ struct virtproc_info {
>   * Every message sent(/received) on the rpmsg bus begins with this header.
>   */
>  struct rpmsg_hdr {
> -       u32 src;
> -       u32 dst;
> -       u32 reserved;
> -       u16 len;
> -       u16 flags;
> +       __virtio32 src;
> +       __virtio32 dst;
> +       __virtio32 reserved;
> +       __virtio16 len;
> +       __virtio16 flags;
>         u8 data[];
>  } __packed;
>
> @@ -106,8 +107,8 @@ struct rpmsg_hdr {
>   */
>  struct rpmsg_ns_msg {
>         char name[RPMSG_NAME_SIZE];
> -       u32 addr;
> -       u32 flags;
> +       __virtio32 addr;
> +       __virtio32 flags;
>  } __packed;
>
>  /**
> @@ -335,8 +336,8 @@ static int virtio_rpmsg_announce_create(struct rpmsg_device *rpdev)
>                 struct rpmsg_ns_msg nsm;
>
>                 strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
> -               nsm.addr = rpdev->ept->addr;
> -               nsm.flags = RPMSG_NS_CREATE;
> +               nsm.addr = cpu_to_virtio32(vrp->vdev, rpdev->ept->addr);
> +               nsm.flags = cpu_to_virtio32(vrp->vdev, RPMSG_NS_CREATE);
>
>                 err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
>                 if (err)
> @@ -359,8 +360,8 @@ static int virtio_rpmsg_announce_destroy(struct rpmsg_device *rpdev)
>                 struct rpmsg_ns_msg nsm;
>
>                 strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
> -               nsm.addr = rpdev->ept->addr;
> -               nsm.flags = RPMSG_NS_DESTROY;
> +               nsm.addr = cpu_to_virtio32(vrp->vdev, rpdev->ept->addr);
> +               nsm.flags = cpu_to_virtio32(vrp->vdev, RPMSG_NS_DESTROY);
>
>                 err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
>                 if (err)
> @@ -612,18 +613,18 @@ static int rpmsg_send_offchannel_raw(struct rpmsg_device *rpdev,
>                 }
>         }
>
> -       msg->len = len;
> +       msg->len = cpu_to_virtio16(vrp->vdev, len);
>         msg->flags = 0;
> -       msg->src = src;
> -       msg->dst = dst;
> +       msg->src = cpu_to_virtio32(vrp->vdev, src);
> +       msg->dst = cpu_to_virtio32(vrp->vdev, dst);
>         msg->reserved = 0;
>         memcpy(msg->data, data, len);
>
>         dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
> -               msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
> +               src, dst, len, msg->flags, msg->reserved);
>  #if defined(CONFIG_DYNAMIC_DEBUG)
>         dynamic_hex_dump("rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
> -                        msg, sizeof(*msg) + msg->len, true);
> +                        msg, sizeof(*msg) + len, true);
>  #endif
>
>         rpmsg_sg_init(&sg, msg, sizeof(*msg) + len);
> @@ -704,13 +705,17 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
>  {
>         struct rpmsg_endpoint *ept;
>         struct scatterlist sg;
> +       unsigned int msg_len = virtio16_to_cpu(vrp->vdev, msg->len);
>         int err;
>
>         dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
> -               msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
> +               virtio32_to_cpu(vrp->vdev, msg->src),
> +               virtio32_to_cpu(vrp->vdev, msg->dst), msg_len,
> +               virtio16_to_cpu(vrp->vdev, msg->flags),
> +               virtio32_to_cpu(vrp->vdev, msg->reserved));
>  #if defined(CONFIG_DYNAMIC_DEBUG)
>         dynamic_hex_dump("rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
> -                        msg, sizeof(*msg) + msg->len, true);
> +                        msg, sizeof(*msg) + msg_len, true);
>  #endif
>
>         /*
> @@ -718,15 +723,15 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
>          * the reported payload length.
>          */
>         if (len > vrp->buf_size ||
> -           msg->len > (len - sizeof(struct rpmsg_hdr))) {
> -               dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg->len);
> +           msg_len > (len - sizeof(struct rpmsg_hdr))) {
> +               dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg_len);
>                 return -EINVAL;
>         }
>
>         /* use the dst addr to fetch the callback of the appropriate user */
>         mutex_lock(&vrp->endpoints_lock);
>
> -       ept = idr_find(&vrp->endpoints, msg->dst);
> +       ept = idr_find(&vrp->endpoints, virtio32_to_cpu(vrp->vdev, msg->dst));
>
>         /* let's make sure no one deallocates ept while we use it */
>         if (ept)
> @@ -739,8 +744,8 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
>                 mutex_lock(&ept->cb_lock);
>
>                 if (ept->cb)
> -                       ept->cb(ept->rpdev, msg->data, msg->len, ept->priv,
> -                               msg->src);
> +                       ept->cb(ept->rpdev, msg->data, msg_len, ept->priv,
> +                               virtio32_to_cpu(vrp->vdev, msg->src));
>
>                 mutex_unlock(&ept->cb_lock);
>
> @@ -846,15 +851,15 @@ static int rpmsg_ns_cb(struct rpmsg_device *rpdev, void *data, int len,
>         /* don't trust the remote processor for null terminating the name */
>         msg->name[RPMSG_NAME_SIZE - 1] = '\0';
>
> -       dev_info(dev, "%sing channel %s addr 0x%x\n",
> -                msg->flags & RPMSG_NS_DESTROY ? "destroy" : "creat",
> -                msg->name, msg->addr);
> -
>         strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
>         chinfo.src = RPMSG_ADDR_ANY;
> -       chinfo.dst = msg->addr;
> +       chinfo.dst = virtio32_to_cpu(vrp->vdev, msg->addr);
> +
> +       dev_info(dev, "%sing channel %s addr 0x%x\n",
> +                virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY ?
> +                "destroy" : "creat", msg->name, chinfo.dst);
>
> -       if (msg->flags & RPMSG_NS_DESTROY) {
> +       if (virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY) {
>                 ret = rpmsg_unregister_device(&vrp->vdev->dev, &chinfo);
>                 if (ret)
>                         dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
> --
> 2.27.0
>

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v4] rpmsg: virtio: add endianness conversions
  2020-07-07 15:38                     ` Mathieu Poirier
@ 2020-07-10  6:20                       ` Guennadi Liakhovetski
  2020-07-21  8:56                       ` [PATCH RESEND " Guennadi Liakhovetski
  1 sibling, 0 replies; 21+ messages in thread
From: Guennadi Liakhovetski @ 2020-07-10  6:20 UTC (permalink / raw)
  To: Mathieu Poirier
  Cc: Arnaud POULIQUEN, Michael S. Tsirkin, linux-remoteproc,
	Ohad Ben-Cohen, Bjorn Andersson, Pierre-Louis Bossart,
	Liam Girdwood

Bjorn, Ohad, is this one good to go? Can we proceed with the next step:

https://marc.info/?l=kvm&m=159060274705978&w=2

Thanks
Guennadi

On Tue, Jul 07, 2020 at 09:38:37AM -0600, Mathieu Poirier wrote:
> On Tue, 7 Jul 2020 at 09:34, Guennadi Liakhovetski
> <guennadi.liakhovetski@linux.intel.com> wrote:
> >
> > According to the VirtIO 1.0 spec data, sent over virtual queues must
> > be in little-endian format. Update the RPMsg VirtIO implementation
> > to enforce that but let legacy configurations continue use native
> > endianness.
> >
> > Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
> > Tested-by: Arnaud Pouliquen <arnaud.pouliquen@st.com>
> > ---
> >
> > Mathieu, I had to remove your "Reviewed-by" because this is a modified version...
> >
> > v4: fix a left-over use of an LE value in calculation - thanks to Arnaud for catching!
> 
> It is indeed a good catch
> 
> Reviewed-by: Mathieu Poirier <mathieu.poirier@linaro.org>
> 
> >
> > v3: use the virtio_byteorder.h header
> >
> > v2: Following suggestions from Michael and Mathieu switch to using virtio16/32
> > types and conversion functions.
> >
> >  drivers/rpmsg/virtio_rpmsg_bus.c | 63 +++++++++++++++++---------------
> >  1 file changed, 34 insertions(+), 29 deletions(-)
> >
> > diff --git a/drivers/rpmsg/virtio_rpmsg_bus.c b/drivers/rpmsg/virtio_rpmsg_bus.c
> > index 07d4f3374098..9006fc7f73d0 100644
> > --- a/drivers/rpmsg/virtio_rpmsg_bus.c
> > +++ b/drivers/rpmsg/virtio_rpmsg_bus.c
> > @@ -23,6 +23,7 @@
> >  #include <linux/slab.h>
> >  #include <linux/sched.h>
> >  #include <linux/virtio.h>
> > +#include <linux/virtio_byteorder.h>
> >  #include <linux/virtio_ids.h>
> >  #include <linux/virtio_config.h>
> >  #include <linux/wait.h>
> > @@ -84,11 +85,11 @@ struct virtproc_info {
> >   * Every message sent(/received) on the rpmsg bus begins with this header.
> >   */
> >  struct rpmsg_hdr {
> > -       u32 src;
> > -       u32 dst;
> > -       u32 reserved;
> > -       u16 len;
> > -       u16 flags;
> > +       __virtio32 src;
> > +       __virtio32 dst;
> > +       __virtio32 reserved;
> > +       __virtio16 len;
> > +       __virtio16 flags;
> >         u8 data[];
> >  } __packed;
> >
> > @@ -106,8 +107,8 @@ struct rpmsg_hdr {
> >   */
> >  struct rpmsg_ns_msg {
> >         char name[RPMSG_NAME_SIZE];
> > -       u32 addr;
> > -       u32 flags;
> > +       __virtio32 addr;
> > +       __virtio32 flags;
> >  } __packed;
> >
> >  /**
> > @@ -335,8 +336,8 @@ static int virtio_rpmsg_announce_create(struct rpmsg_device *rpdev)
> >                 struct rpmsg_ns_msg nsm;
> >
> >                 strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
> > -               nsm.addr = rpdev->ept->addr;
> > -               nsm.flags = RPMSG_NS_CREATE;
> > +               nsm.addr = cpu_to_virtio32(vrp->vdev, rpdev->ept->addr);
> > +               nsm.flags = cpu_to_virtio32(vrp->vdev, RPMSG_NS_CREATE);
> >
> >                 err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
> >                 if (err)
> > @@ -359,8 +360,8 @@ static int virtio_rpmsg_announce_destroy(struct rpmsg_device *rpdev)
> >                 struct rpmsg_ns_msg nsm;
> >
> >                 strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
> > -               nsm.addr = rpdev->ept->addr;
> > -               nsm.flags = RPMSG_NS_DESTROY;
> > +               nsm.addr = cpu_to_virtio32(vrp->vdev, rpdev->ept->addr);
> > +               nsm.flags = cpu_to_virtio32(vrp->vdev, RPMSG_NS_DESTROY);
> >
> >                 err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
> >                 if (err)
> > @@ -612,18 +613,18 @@ static int rpmsg_send_offchannel_raw(struct rpmsg_device *rpdev,
> >                 }
> >         }
> >
> > -       msg->len = len;
> > +       msg->len = cpu_to_virtio16(vrp->vdev, len);
> >         msg->flags = 0;
> > -       msg->src = src;
> > -       msg->dst = dst;
> > +       msg->src = cpu_to_virtio32(vrp->vdev, src);
> > +       msg->dst = cpu_to_virtio32(vrp->vdev, dst);
> >         msg->reserved = 0;
> >         memcpy(msg->data, data, len);
> >
> >         dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
> > -               msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
> > +               src, dst, len, msg->flags, msg->reserved);
> >  #if defined(CONFIG_DYNAMIC_DEBUG)
> >         dynamic_hex_dump("rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
> > -                        msg, sizeof(*msg) + msg->len, true);
> > +                        msg, sizeof(*msg) + len, true);
> >  #endif
> >
> >         rpmsg_sg_init(&sg, msg, sizeof(*msg) + len);
> > @@ -704,13 +705,17 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
> >  {
> >         struct rpmsg_endpoint *ept;
> >         struct scatterlist sg;
> > +       unsigned int msg_len = virtio16_to_cpu(vrp->vdev, msg->len);
> >         int err;
> >
> >         dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
> > -               msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
> > +               virtio32_to_cpu(vrp->vdev, msg->src),
> > +               virtio32_to_cpu(vrp->vdev, msg->dst), msg_len,
> > +               virtio16_to_cpu(vrp->vdev, msg->flags),
> > +               virtio32_to_cpu(vrp->vdev, msg->reserved));
> >  #if defined(CONFIG_DYNAMIC_DEBUG)
> >         dynamic_hex_dump("rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
> > -                        msg, sizeof(*msg) + msg->len, true);
> > +                        msg, sizeof(*msg) + msg_len, true);
> >  #endif
> >
> >         /*
> > @@ -718,15 +723,15 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
> >          * the reported payload length.
> >          */
> >         if (len > vrp->buf_size ||
> > -           msg->len > (len - sizeof(struct rpmsg_hdr))) {
> > -               dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg->len);
> > +           msg_len > (len - sizeof(struct rpmsg_hdr))) {
> > +               dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg_len);
> >                 return -EINVAL;
> >         }
> >
> >         /* use the dst addr to fetch the callback of the appropriate user */
> >         mutex_lock(&vrp->endpoints_lock);
> >
> > -       ept = idr_find(&vrp->endpoints, msg->dst);
> > +       ept = idr_find(&vrp->endpoints, virtio32_to_cpu(vrp->vdev, msg->dst));
> >
> >         /* let's make sure no one deallocates ept while we use it */
> >         if (ept)
> > @@ -739,8 +744,8 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
> >                 mutex_lock(&ept->cb_lock);
> >
> >                 if (ept->cb)
> > -                       ept->cb(ept->rpdev, msg->data, msg->len, ept->priv,
> > -                               msg->src);
> > +                       ept->cb(ept->rpdev, msg->data, msg_len, ept->priv,
> > +                               virtio32_to_cpu(vrp->vdev, msg->src));
> >
> >                 mutex_unlock(&ept->cb_lock);
> >
> > @@ -846,15 +851,15 @@ static int rpmsg_ns_cb(struct rpmsg_device *rpdev, void *data, int len,
> >         /* don't trust the remote processor for null terminating the name */
> >         msg->name[RPMSG_NAME_SIZE - 1] = '\0';
> >
> > -       dev_info(dev, "%sing channel %s addr 0x%x\n",
> > -                msg->flags & RPMSG_NS_DESTROY ? "destroy" : "creat",
> > -                msg->name, msg->addr);
> > -
> >         strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
> >         chinfo.src = RPMSG_ADDR_ANY;
> > -       chinfo.dst = msg->addr;
> > +       chinfo.dst = virtio32_to_cpu(vrp->vdev, msg->addr);
> > +
> > +       dev_info(dev, "%sing channel %s addr 0x%x\n",
> > +                virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY ?
> > +                "destroy" : "creat", msg->name, chinfo.dst);
> >
> > -       if (msg->flags & RPMSG_NS_DESTROY) {
> > +       if (virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY) {
> >                 ret = rpmsg_unregister_device(&vrp->vdev->dev, &chinfo);
> >                 if (ret)
> >                         dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
> > --
> > 2.27.0
> >

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [PATCH RESEND v4] rpmsg: virtio: add endianness conversions
  2020-07-07 15:38                     ` Mathieu Poirier
  2020-07-10  6:20                       ` Guennadi Liakhovetski
@ 2020-07-21  8:56                       ` Guennadi Liakhovetski
  2020-07-21 10:04                         ` Michael S. Tsirkin
                                           ` (3 more replies)
  1 sibling, 4 replies; 21+ messages in thread
From: Guennadi Liakhovetski @ 2020-07-21  8:56 UTC (permalink / raw)
  To: Mathieu Poirier
  Cc: Arnaud POULIQUEN, Michael S. Tsirkin, linux-remoteproc,
	Ohad Ben-Cohen, Bjorn Andersson, Pierre-Louis Bossart,
	Liam Girdwood

According to the VirtIO 1.0 spec data, sent over virtual queues must
be in little-endian format. Update the RPMsg VirtIO implementation
to enforce that but let legacy configurations continue use native
endianness.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
Reviewed-by: Mathieu Poirier <mathieu.poirier@linaro.org>
Tested-by: Arnaud Pouliquen <arnaud.pouliquen@st.com>
---

Ping: 2 weeks since the original submission. Also added back the updated
"reviewed-by" tag.

v4: fix a left-over use of an LE value in calculation - thanks to Arnaud for
catching!

v3: use the virtio_byteorder.h header

v2: Following suggestions from Michael and Mathieu switch to using virtio16/32
types and conversion functions.

 drivers/rpmsg/virtio_rpmsg_bus.c | 63 +++++++++++++++++---------------
 1 file changed, 34 insertions(+), 29 deletions(-)

diff --git a/drivers/rpmsg/virtio_rpmsg_bus.c b/drivers/rpmsg/virtio_rpmsg_bus.c
index 07d4f3374098..9006fc7f73d0 100644
--- a/drivers/rpmsg/virtio_rpmsg_bus.c
+++ b/drivers/rpmsg/virtio_rpmsg_bus.c
@@ -23,6 +23,7 @@
 #include <linux/slab.h>
 #include <linux/sched.h>
 #include <linux/virtio.h>
+#include <linux/virtio_byteorder.h>
 #include <linux/virtio_ids.h>
 #include <linux/virtio_config.h>
 #include <linux/wait.h>
@@ -84,11 +85,11 @@ struct virtproc_info {
  * Every message sent(/received) on the rpmsg bus begins with this header.
  */
 struct rpmsg_hdr {
-	u32 src;
-	u32 dst;
-	u32 reserved;
-	u16 len;
-	u16 flags;
+	__virtio32 src;
+	__virtio32 dst;
+	__virtio32 reserved;
+	__virtio16 len;
+	__virtio16 flags;
 	u8 data[];
 } __packed;
 
@@ -106,8 +107,8 @@ struct rpmsg_hdr {
  */
 struct rpmsg_ns_msg {
 	char name[RPMSG_NAME_SIZE];
-	u32 addr;
-	u32 flags;
+	__virtio32 addr;
+	__virtio32 flags;
 } __packed;
 
 /**
@@ -335,8 +336,8 @@ static int virtio_rpmsg_announce_create(struct rpmsg_device *rpdev)
 		struct rpmsg_ns_msg nsm;
 
 		strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
-		nsm.addr = rpdev->ept->addr;
-		nsm.flags = RPMSG_NS_CREATE;
+		nsm.addr = cpu_to_virtio32(vrp->vdev, rpdev->ept->addr);
+		nsm.flags = cpu_to_virtio32(vrp->vdev, RPMSG_NS_CREATE);
 
 		err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
 		if (err)
@@ -359,8 +360,8 @@ static int virtio_rpmsg_announce_destroy(struct rpmsg_device *rpdev)
 		struct rpmsg_ns_msg nsm;
 
 		strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
-		nsm.addr = rpdev->ept->addr;
-		nsm.flags = RPMSG_NS_DESTROY;
+		nsm.addr = cpu_to_virtio32(vrp->vdev, rpdev->ept->addr);
+		nsm.flags = cpu_to_virtio32(vrp->vdev, RPMSG_NS_DESTROY);
 
 		err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
 		if (err)
@@ -612,18 +613,18 @@ static int rpmsg_send_offchannel_raw(struct rpmsg_device *rpdev,
 		}
 	}
 
-	msg->len = len;
+	msg->len = cpu_to_virtio16(vrp->vdev, len);
 	msg->flags = 0;
-	msg->src = src;
-	msg->dst = dst;
+	msg->src = cpu_to_virtio32(vrp->vdev, src);
+	msg->dst = cpu_to_virtio32(vrp->vdev, dst);
 	msg->reserved = 0;
 	memcpy(msg->data, data, len);
 
 	dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
-		msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
+		src, dst, len, msg->flags, msg->reserved);
 #if defined(CONFIG_DYNAMIC_DEBUG)
 	dynamic_hex_dump("rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
-			 msg, sizeof(*msg) + msg->len, true);
+			 msg, sizeof(*msg) + len, true);
 #endif
 
 	rpmsg_sg_init(&sg, msg, sizeof(*msg) + len);
@@ -704,13 +705,17 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
 {
 	struct rpmsg_endpoint *ept;
 	struct scatterlist sg;
+	unsigned int msg_len = virtio16_to_cpu(vrp->vdev, msg->len);
 	int err;
 
 	dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
-		msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
+		virtio32_to_cpu(vrp->vdev, msg->src),
+		virtio32_to_cpu(vrp->vdev, msg->dst), msg_len,
+		virtio16_to_cpu(vrp->vdev, msg->flags),
+		virtio32_to_cpu(vrp->vdev, msg->reserved));
 #if defined(CONFIG_DYNAMIC_DEBUG)
 	dynamic_hex_dump("rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
-			 msg, sizeof(*msg) + msg->len, true);
+			 msg, sizeof(*msg) + msg_len, true);
 #endif
 
 	/*
@@ -718,15 +723,15 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
 	 * the reported payload length.
 	 */
 	if (len > vrp->buf_size ||
-	    msg->len > (len - sizeof(struct rpmsg_hdr))) {
-		dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg->len);
+	    msg_len > (len - sizeof(struct rpmsg_hdr))) {
+		dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg_len);
 		return -EINVAL;
 	}
 
 	/* use the dst addr to fetch the callback of the appropriate user */
 	mutex_lock(&vrp->endpoints_lock);
 
-	ept = idr_find(&vrp->endpoints, msg->dst);
+	ept = idr_find(&vrp->endpoints, virtio32_to_cpu(vrp->vdev, msg->dst));
 
 	/* let's make sure no one deallocates ept while we use it */
 	if (ept)
@@ -739,8 +744,8 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
 		mutex_lock(&ept->cb_lock);
 
 		if (ept->cb)
-			ept->cb(ept->rpdev, msg->data, msg->len, ept->priv,
-				msg->src);
+			ept->cb(ept->rpdev, msg->data, msg_len, ept->priv,
+				virtio32_to_cpu(vrp->vdev, msg->src));
 
 		mutex_unlock(&ept->cb_lock);
 
@@ -846,15 +851,15 @@ static int rpmsg_ns_cb(struct rpmsg_device *rpdev, void *data, int len,
 	/* don't trust the remote processor for null terminating the name */
 	msg->name[RPMSG_NAME_SIZE - 1] = '\0';
 
-	dev_info(dev, "%sing channel %s addr 0x%x\n",
-		 msg->flags & RPMSG_NS_DESTROY ? "destroy" : "creat",
-		 msg->name, msg->addr);
-
 	strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
 	chinfo.src = RPMSG_ADDR_ANY;
-	chinfo.dst = msg->addr;
+	chinfo.dst = virtio32_to_cpu(vrp->vdev, msg->addr);
+
+	dev_info(dev, "%sing channel %s addr 0x%x\n",
+		 virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY ?
+		 "destroy" : "creat", msg->name, chinfo.dst);
 
-	if (msg->flags & RPMSG_NS_DESTROY) {
+	if (virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY) {
 		ret = rpmsg_unregister_device(&vrp->vdev->dev, &chinfo);
 		if (ret)
 			dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
-- 
2.27.0


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH RESEND v4] rpmsg: virtio: add endianness conversions
  2020-07-21  8:56                       ` [PATCH RESEND " Guennadi Liakhovetski
@ 2020-07-21 10:04                         ` Michael S. Tsirkin
  2020-07-22  3:39                         ` Bjorn Andersson
                                           ` (2 subsequent siblings)
  3 siblings, 0 replies; 21+ messages in thread
From: Michael S. Tsirkin @ 2020-07-21 10:04 UTC (permalink / raw)
  To: Guennadi Liakhovetski
  Cc: Mathieu Poirier, Arnaud POULIQUEN, linux-remoteproc,
	Ohad Ben-Cohen, Bjorn Andersson, Pierre-Louis Bossart,
	Liam Girdwood

On Tue, Jul 21, 2020 at 10:56:38AM +0200, Guennadi Liakhovetski wrote:
> According to the VirtIO 1.0 spec data, sent over virtual queues must
> be in little-endian format. Update the RPMsg VirtIO implementation
> to enforce that but let legacy configurations continue use native
> endianness.
> 
> Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
> Reviewed-by: Mathieu Poirier <mathieu.poirier@linaro.org>
> Tested-by: Arnaud Pouliquen <arnaud.pouliquen@st.com>

Looks good to me
Acked-by: Michael S. Tsirkin <mst@redhat.com>

Who's merging this? If me, could I get acks from maintainers on this please?


> ---
> 
> Ping: 2 weeks since the original submission. Also added back the updated
> "reviewed-by" tag.
> 
> v4: fix a left-over use of an LE value in calculation - thanks to Arnaud for
> catching!
> 
> v3: use the virtio_byteorder.h header
> 
> v2: Following suggestions from Michael and Mathieu switch to using virtio16/32
> types and conversion functions.
> 
>  drivers/rpmsg/virtio_rpmsg_bus.c | 63 +++++++++++++++++---------------
>  1 file changed, 34 insertions(+), 29 deletions(-)
> 
> diff --git a/drivers/rpmsg/virtio_rpmsg_bus.c b/drivers/rpmsg/virtio_rpmsg_bus.c
> index 07d4f3374098..9006fc7f73d0 100644
> --- a/drivers/rpmsg/virtio_rpmsg_bus.c
> +++ b/drivers/rpmsg/virtio_rpmsg_bus.c
> @@ -23,6 +23,7 @@
>  #include <linux/slab.h>
>  #include <linux/sched.h>
>  #include <linux/virtio.h>
> +#include <linux/virtio_byteorder.h>
>  #include <linux/virtio_ids.h>
>  #include <linux/virtio_config.h>
>  #include <linux/wait.h>
> @@ -84,11 +85,11 @@ struct virtproc_info {
>   * Every message sent(/received) on the rpmsg bus begins with this header.
>   */
>  struct rpmsg_hdr {
> -	u32 src;
> -	u32 dst;
> -	u32 reserved;
> -	u16 len;
> -	u16 flags;
> +	__virtio32 src;
> +	__virtio32 dst;
> +	__virtio32 reserved;
> +	__virtio16 len;
> +	__virtio16 flags;
>  	u8 data[];
>  } __packed;
>  
> @@ -106,8 +107,8 @@ struct rpmsg_hdr {
>   */
>  struct rpmsg_ns_msg {
>  	char name[RPMSG_NAME_SIZE];
> -	u32 addr;
> -	u32 flags;
> +	__virtio32 addr;
> +	__virtio32 flags;
>  } __packed;
>  
>  /**
> @@ -335,8 +336,8 @@ static int virtio_rpmsg_announce_create(struct rpmsg_device *rpdev)
>  		struct rpmsg_ns_msg nsm;
>  
>  		strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
> -		nsm.addr = rpdev->ept->addr;
> -		nsm.flags = RPMSG_NS_CREATE;
> +		nsm.addr = cpu_to_virtio32(vrp->vdev, rpdev->ept->addr);
> +		nsm.flags = cpu_to_virtio32(vrp->vdev, RPMSG_NS_CREATE);
>  
>  		err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
>  		if (err)
> @@ -359,8 +360,8 @@ static int virtio_rpmsg_announce_destroy(struct rpmsg_device *rpdev)
>  		struct rpmsg_ns_msg nsm;
>  
>  		strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
> -		nsm.addr = rpdev->ept->addr;
> -		nsm.flags = RPMSG_NS_DESTROY;
> +		nsm.addr = cpu_to_virtio32(vrp->vdev, rpdev->ept->addr);
> +		nsm.flags = cpu_to_virtio32(vrp->vdev, RPMSG_NS_DESTROY);
>  
>  		err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
>  		if (err)
> @@ -612,18 +613,18 @@ static int rpmsg_send_offchannel_raw(struct rpmsg_device *rpdev,
>  		}
>  	}
>  
> -	msg->len = len;
> +	msg->len = cpu_to_virtio16(vrp->vdev, len);
>  	msg->flags = 0;
> -	msg->src = src;
> -	msg->dst = dst;
> +	msg->src = cpu_to_virtio32(vrp->vdev, src);
> +	msg->dst = cpu_to_virtio32(vrp->vdev, dst);
>  	msg->reserved = 0;
>  	memcpy(msg->data, data, len);
>  
>  	dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
> -		msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
> +		src, dst, len, msg->flags, msg->reserved);
>  #if defined(CONFIG_DYNAMIC_DEBUG)
>  	dynamic_hex_dump("rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
> -			 msg, sizeof(*msg) + msg->len, true);
> +			 msg, sizeof(*msg) + len, true);
>  #endif
>  
>  	rpmsg_sg_init(&sg, msg, sizeof(*msg) + len);
> @@ -704,13 +705,17 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
>  {
>  	struct rpmsg_endpoint *ept;
>  	struct scatterlist sg;
> +	unsigned int msg_len = virtio16_to_cpu(vrp->vdev, msg->len);
>  	int err;
>  
>  	dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
> -		msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
> +		virtio32_to_cpu(vrp->vdev, msg->src),
> +		virtio32_to_cpu(vrp->vdev, msg->dst), msg_len,
> +		virtio16_to_cpu(vrp->vdev, msg->flags),
> +		virtio32_to_cpu(vrp->vdev, msg->reserved));
>  #if defined(CONFIG_DYNAMIC_DEBUG)
>  	dynamic_hex_dump("rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
> -			 msg, sizeof(*msg) + msg->len, true);
> +			 msg, sizeof(*msg) + msg_len, true);
>  #endif
>  
>  	/*
> @@ -718,15 +723,15 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
>  	 * the reported payload length.
>  	 */
>  	if (len > vrp->buf_size ||
> -	    msg->len > (len - sizeof(struct rpmsg_hdr))) {
> -		dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg->len);
> +	    msg_len > (len - sizeof(struct rpmsg_hdr))) {
> +		dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg_len);
>  		return -EINVAL;
>  	}
>  
>  	/* use the dst addr to fetch the callback of the appropriate user */
>  	mutex_lock(&vrp->endpoints_lock);
>  
> -	ept = idr_find(&vrp->endpoints, msg->dst);
> +	ept = idr_find(&vrp->endpoints, virtio32_to_cpu(vrp->vdev, msg->dst));
>  
>  	/* let's make sure no one deallocates ept while we use it */
>  	if (ept)
> @@ -739,8 +744,8 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
>  		mutex_lock(&ept->cb_lock);
>  
>  		if (ept->cb)
> -			ept->cb(ept->rpdev, msg->data, msg->len, ept->priv,
> -				msg->src);
> +			ept->cb(ept->rpdev, msg->data, msg_len, ept->priv,
> +				virtio32_to_cpu(vrp->vdev, msg->src));
>  
>  		mutex_unlock(&ept->cb_lock);
>  
> @@ -846,15 +851,15 @@ static int rpmsg_ns_cb(struct rpmsg_device *rpdev, void *data, int len,
>  	/* don't trust the remote processor for null terminating the name */
>  	msg->name[RPMSG_NAME_SIZE - 1] = '\0';
>  
> -	dev_info(dev, "%sing channel %s addr 0x%x\n",
> -		 msg->flags & RPMSG_NS_DESTROY ? "destroy" : "creat",
> -		 msg->name, msg->addr);
> -
>  	strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
>  	chinfo.src = RPMSG_ADDR_ANY;
> -	chinfo.dst = msg->addr;
> +	chinfo.dst = virtio32_to_cpu(vrp->vdev, msg->addr);
> +
> +	dev_info(dev, "%sing channel %s addr 0x%x\n",
> +		 virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY ?
> +		 "destroy" : "creat", msg->name, chinfo.dst);
>  
> -	if (msg->flags & RPMSG_NS_DESTROY) {
> +	if (virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY) {
>  		ret = rpmsg_unregister_device(&vrp->vdev->dev, &chinfo);
>  		if (ret)
>  			dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
> -- 
> 2.27.0


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH RESEND v4] rpmsg: virtio: add endianness conversions
  2020-07-21  8:56                       ` [PATCH RESEND " Guennadi Liakhovetski
  2020-07-21 10:04                         ` Michael S. Tsirkin
@ 2020-07-22  3:39                         ` Bjorn Andersson
  2020-07-22  4:00                         ` patchwork-bot+linux-remoteproc
  2020-07-27 16:07                         ` Michael S. Tsirkin
  3 siblings, 0 replies; 21+ messages in thread
From: Bjorn Andersson @ 2020-07-22  3:39 UTC (permalink / raw)
  To: Guennadi Liakhovetski
  Cc: Mathieu Poirier, Arnaud POULIQUEN, Michael S. Tsirkin,
	linux-remoteproc, Ohad Ben-Cohen, Pierre-Louis Bossart,
	Liam Girdwood

On Tue 21 Jul 01:56 PDT 2020, Guennadi Liakhovetski wrote:

> According to the VirtIO 1.0 spec data, sent over virtual queues must
> be in little-endian format. Update the RPMsg VirtIO implementation
> to enforce that but let legacy configurations continue use native
> endianness.
> 
> Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
> Reviewed-by: Mathieu Poirier <mathieu.poirier@linaro.org>
> Tested-by: Arnaud Pouliquen <arnaud.pouliquen@st.com>
> ---
> 
> Ping: 2 weeks since the original submission. Also added back the updated
> "reviewed-by" tag.
> 

Sorry about that, I missed the reviewed version in my inbox. Applied to
the rpmsg-next tree for v5.9, with Michael's ack.

Regards,
Bjorn

> v4: fix a left-over use of an LE value in calculation - thanks to Arnaud for
> catching!
> 
> v3: use the virtio_byteorder.h header
> 
> v2: Following suggestions from Michael and Mathieu switch to using virtio16/32
> types and conversion functions.
> 
>  drivers/rpmsg/virtio_rpmsg_bus.c | 63 +++++++++++++++++---------------
>  1 file changed, 34 insertions(+), 29 deletions(-)
> 
> diff --git a/drivers/rpmsg/virtio_rpmsg_bus.c b/drivers/rpmsg/virtio_rpmsg_bus.c
> index 07d4f3374098..9006fc7f73d0 100644
> --- a/drivers/rpmsg/virtio_rpmsg_bus.c
> +++ b/drivers/rpmsg/virtio_rpmsg_bus.c
> @@ -23,6 +23,7 @@
>  #include <linux/slab.h>
>  #include <linux/sched.h>
>  #include <linux/virtio.h>
> +#include <linux/virtio_byteorder.h>
>  #include <linux/virtio_ids.h>
>  #include <linux/virtio_config.h>
>  #include <linux/wait.h>
> @@ -84,11 +85,11 @@ struct virtproc_info {
>   * Every message sent(/received) on the rpmsg bus begins with this header.
>   */
>  struct rpmsg_hdr {
> -	u32 src;
> -	u32 dst;
> -	u32 reserved;
> -	u16 len;
> -	u16 flags;
> +	__virtio32 src;
> +	__virtio32 dst;
> +	__virtio32 reserved;
> +	__virtio16 len;
> +	__virtio16 flags;
>  	u8 data[];
>  } __packed;
>  
> @@ -106,8 +107,8 @@ struct rpmsg_hdr {
>   */
>  struct rpmsg_ns_msg {
>  	char name[RPMSG_NAME_SIZE];
> -	u32 addr;
> -	u32 flags;
> +	__virtio32 addr;
> +	__virtio32 flags;
>  } __packed;
>  
>  /**
> @@ -335,8 +336,8 @@ static int virtio_rpmsg_announce_create(struct rpmsg_device *rpdev)
>  		struct rpmsg_ns_msg nsm;
>  
>  		strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
> -		nsm.addr = rpdev->ept->addr;
> -		nsm.flags = RPMSG_NS_CREATE;
> +		nsm.addr = cpu_to_virtio32(vrp->vdev, rpdev->ept->addr);
> +		nsm.flags = cpu_to_virtio32(vrp->vdev, RPMSG_NS_CREATE);
>  
>  		err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
>  		if (err)
> @@ -359,8 +360,8 @@ static int virtio_rpmsg_announce_destroy(struct rpmsg_device *rpdev)
>  		struct rpmsg_ns_msg nsm;
>  
>  		strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
> -		nsm.addr = rpdev->ept->addr;
> -		nsm.flags = RPMSG_NS_DESTROY;
> +		nsm.addr = cpu_to_virtio32(vrp->vdev, rpdev->ept->addr);
> +		nsm.flags = cpu_to_virtio32(vrp->vdev, RPMSG_NS_DESTROY);
>  
>  		err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
>  		if (err)
> @@ -612,18 +613,18 @@ static int rpmsg_send_offchannel_raw(struct rpmsg_device *rpdev,
>  		}
>  	}
>  
> -	msg->len = len;
> +	msg->len = cpu_to_virtio16(vrp->vdev, len);
>  	msg->flags = 0;
> -	msg->src = src;
> -	msg->dst = dst;
> +	msg->src = cpu_to_virtio32(vrp->vdev, src);
> +	msg->dst = cpu_to_virtio32(vrp->vdev, dst);
>  	msg->reserved = 0;
>  	memcpy(msg->data, data, len);
>  
>  	dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
> -		msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
> +		src, dst, len, msg->flags, msg->reserved);
>  #if defined(CONFIG_DYNAMIC_DEBUG)
>  	dynamic_hex_dump("rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
> -			 msg, sizeof(*msg) + msg->len, true);
> +			 msg, sizeof(*msg) + len, true);
>  #endif
>  
>  	rpmsg_sg_init(&sg, msg, sizeof(*msg) + len);
> @@ -704,13 +705,17 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
>  {
>  	struct rpmsg_endpoint *ept;
>  	struct scatterlist sg;
> +	unsigned int msg_len = virtio16_to_cpu(vrp->vdev, msg->len);
>  	int err;
>  
>  	dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
> -		msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
> +		virtio32_to_cpu(vrp->vdev, msg->src),
> +		virtio32_to_cpu(vrp->vdev, msg->dst), msg_len,
> +		virtio16_to_cpu(vrp->vdev, msg->flags),
> +		virtio32_to_cpu(vrp->vdev, msg->reserved));
>  #if defined(CONFIG_DYNAMIC_DEBUG)
>  	dynamic_hex_dump("rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
> -			 msg, sizeof(*msg) + msg->len, true);
> +			 msg, sizeof(*msg) + msg_len, true);
>  #endif
>  
>  	/*
> @@ -718,15 +723,15 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
>  	 * the reported payload length.
>  	 */
>  	if (len > vrp->buf_size ||
> -	    msg->len > (len - sizeof(struct rpmsg_hdr))) {
> -		dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg->len);
> +	    msg_len > (len - sizeof(struct rpmsg_hdr))) {
> +		dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg_len);
>  		return -EINVAL;
>  	}
>  
>  	/* use the dst addr to fetch the callback of the appropriate user */
>  	mutex_lock(&vrp->endpoints_lock);
>  
> -	ept = idr_find(&vrp->endpoints, msg->dst);
> +	ept = idr_find(&vrp->endpoints, virtio32_to_cpu(vrp->vdev, msg->dst));
>  
>  	/* let's make sure no one deallocates ept while we use it */
>  	if (ept)
> @@ -739,8 +744,8 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
>  		mutex_lock(&ept->cb_lock);
>  
>  		if (ept->cb)
> -			ept->cb(ept->rpdev, msg->data, msg->len, ept->priv,
> -				msg->src);
> +			ept->cb(ept->rpdev, msg->data, msg_len, ept->priv,
> +				virtio32_to_cpu(vrp->vdev, msg->src));
>  
>  		mutex_unlock(&ept->cb_lock);
>  
> @@ -846,15 +851,15 @@ static int rpmsg_ns_cb(struct rpmsg_device *rpdev, void *data, int len,
>  	/* don't trust the remote processor for null terminating the name */
>  	msg->name[RPMSG_NAME_SIZE - 1] = '\0';
>  
> -	dev_info(dev, "%sing channel %s addr 0x%x\n",
> -		 msg->flags & RPMSG_NS_DESTROY ? "destroy" : "creat",
> -		 msg->name, msg->addr);
> -
>  	strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
>  	chinfo.src = RPMSG_ADDR_ANY;
> -	chinfo.dst = msg->addr;
> +	chinfo.dst = virtio32_to_cpu(vrp->vdev, msg->addr);
> +
> +	dev_info(dev, "%sing channel %s addr 0x%x\n",
> +		 virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY ?
> +		 "destroy" : "creat", msg->name, chinfo.dst);
>  
> -	if (msg->flags & RPMSG_NS_DESTROY) {
> +	if (virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY) {
>  		ret = rpmsg_unregister_device(&vrp->vdev->dev, &chinfo);
>  		if (ret)
>  			dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
> -- 
> 2.27.0
> 

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH RESEND v4] rpmsg: virtio: add endianness conversions
  2020-07-21  8:56                       ` [PATCH RESEND " Guennadi Liakhovetski
  2020-07-21 10:04                         ` Michael S. Tsirkin
  2020-07-22  3:39                         ` Bjorn Andersson
@ 2020-07-22  4:00                         ` patchwork-bot+linux-remoteproc
  2020-07-27 16:07                         ` Michael S. Tsirkin
  3 siblings, 0 replies; 21+ messages in thread
From: patchwork-bot+linux-remoteproc @ 2020-07-22  4:00 UTC (permalink / raw)
  To: Guennadi Liakhovetski; +Cc: linux-remoteproc

Hello:

This patch was applied to andersson/remoteproc.git (refs/heads/for-next).

On Tue, 21 Jul 2020 10:56:38 +0200 you wrote:
> According to the VirtIO 1.0 spec data, sent over virtual queues must
> be in little-endian format. Update the RPMsg VirtIO implementation
> to enforce that but let legacy configurations continue use native
> endianness.
> 
> Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
> Reviewed-by: Mathieu Poirier <mathieu.poirier@linaro.org>
> Tested-by: Arnaud Pouliquen <arnaud.pouliquen@st.com>
> 
> [...]


Here is a summary with links:
  - [RESEND,v4] rpmsg: virtio: add endianness conversions
    https://git.kernel.org/andersson/remoteproc/c/111d1089700cdb752681ef44f54ab6137736f5c2

You are awesome, thank you!

-- 
Deet-doot-dot, I am a bot.
https://korg.wiki.kernel.org/userdoc/pwbot

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH RESEND v4] rpmsg: virtio: add endianness conversions
  2020-07-21  8:56                       ` [PATCH RESEND " Guennadi Liakhovetski
                                           ` (2 preceding siblings ...)
  2020-07-22  4:00                         ` patchwork-bot+linux-remoteproc
@ 2020-07-27 16:07                         ` Michael S. Tsirkin
  2020-07-27 16:09                           ` Mathieu Poirier
  3 siblings, 1 reply; 21+ messages in thread
From: Michael S. Tsirkin @ 2020-07-27 16:07 UTC (permalink / raw)
  To: Guennadi Liakhovetski
  Cc: Mathieu Poirier, Arnaud POULIQUEN, linux-remoteproc,
	Ohad Ben-Cohen, Bjorn Andersson, Pierre-Louis Bossart,
	Liam Girdwood

On Tue, Jul 21, 2020 at 10:56:38AM +0200, Guennadi Liakhovetski wrote:
> According to the VirtIO 1.0 spec data, sent over virtual queues must
> be in little-endian format. Update the RPMsg VirtIO implementation
> to enforce that but let legacy configurations continue use native
> endianness.
> 
> Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
> Reviewed-by: Mathieu Poirier <mathieu.poirier@linaro.org>
> Tested-by: Arnaud Pouliquen <arnaud.pouliquen@st.com>

> ---
> 
> Ping: 2 weeks since the original submission. Also added back the updated
> "reviewed-by" tag.
> 
> v4: fix a left-over use of an LE value in calculation - thanks to Arnaud for
> catching!
> 
> v3: use the virtio_byteorder.h header
> 
> v2: Following suggestions from Michael and Mathieu switch to using virtio16/32
> types and conversion functions.
> 
>  drivers/rpmsg/virtio_rpmsg_bus.c | 63 +++++++++++++++++---------------
>  1 file changed, 34 insertions(+), 29 deletions(-)
> 
> diff --git a/drivers/rpmsg/virtio_rpmsg_bus.c b/drivers/rpmsg/virtio_rpmsg_bus.c
> index 07d4f3374098..9006fc7f73d0 100644
> --- a/drivers/rpmsg/virtio_rpmsg_bus.c
> +++ b/drivers/rpmsg/virtio_rpmsg_bus.c
> @@ -23,6 +23,7 @@
>  #include <linux/slab.h>
>  #include <linux/sched.h>
>  #include <linux/virtio.h>
> +#include <linux/virtio_byteorder.h>
>  #include <linux/virtio_ids.h>
>  #include <linux/virtio_config.h>
>  #include <linux/wait.h>
> @@ -84,11 +85,11 @@ struct virtproc_info {
>   * Every message sent(/received) on the rpmsg bus begins with this header.
>   */
>  struct rpmsg_hdr {
> -	u32 src;
> -	u32 dst;
> -	u32 reserved;
> -	u16 len;
> -	u16 flags;
> +	__virtio32 src;
> +	__virtio32 dst;
> +	__virtio32 reserved;
> +	__virtio16 len;
> +	__virtio16 flags;
>  	u8 data[];
>  } __packed;
>  
> @@ -106,8 +107,8 @@ struct rpmsg_hdr {
>   */
>  struct rpmsg_ns_msg {
>  	char name[RPMSG_NAME_SIZE];
> -	u32 addr;
> -	u32 flags;
> +	__virtio32 addr;
> +	__virtio32 flags;
>  } __packed;
>  
>  /**
> @@ -335,8 +336,8 @@ static int virtio_rpmsg_announce_create(struct rpmsg_device *rpdev)
>  		struct rpmsg_ns_msg nsm;
>  
>  		strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
> -		nsm.addr = rpdev->ept->addr;
> -		nsm.flags = RPMSG_NS_CREATE;
> +		nsm.addr = cpu_to_virtio32(vrp->vdev, rpdev->ept->addr);
> +		nsm.flags = cpu_to_virtio32(vrp->vdev, RPMSG_NS_CREATE);
>  
>  		err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
>  		if (err)
> @@ -359,8 +360,8 @@ static int virtio_rpmsg_announce_destroy(struct rpmsg_device *rpdev)
>  		struct rpmsg_ns_msg nsm;
>  
>  		strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
> -		nsm.addr = rpdev->ept->addr;
> -		nsm.flags = RPMSG_NS_DESTROY;
> +		nsm.addr = cpu_to_virtio32(vrp->vdev, rpdev->ept->addr);
> +		nsm.flags = cpu_to_virtio32(vrp->vdev, RPMSG_NS_DESTROY);
>  
>  		err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
>  		if (err)
> @@ -612,18 +613,18 @@ static int rpmsg_send_offchannel_raw(struct rpmsg_device *rpdev,
>  		}
>  	}
>  
> -	msg->len = len;
> +	msg->len = cpu_to_virtio16(vrp->vdev, len);
>  	msg->flags = 0;
> -	msg->src = src;
> -	msg->dst = dst;
> +	msg->src = cpu_to_virtio32(vrp->vdev, src);
> +	msg->dst = cpu_to_virtio32(vrp->vdev, dst);
>  	msg->reserved = 0;
>  	memcpy(msg->data, data, len);
>  
>  	dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
> -		msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
> +		src, dst, len, msg->flags, msg->reserved);
>  #if defined(CONFIG_DYNAMIC_DEBUG)
>  	dynamic_hex_dump("rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
> -			 msg, sizeof(*msg) + msg->len, true);
> +			 msg, sizeof(*msg) + len, true);
>  #endif
>  
>  	rpmsg_sg_init(&sg, msg, sizeof(*msg) + len);
> @@ -704,13 +705,17 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
>  {
>  	struct rpmsg_endpoint *ept;
>  	struct scatterlist sg;
> +	unsigned int msg_len = virtio16_to_cpu(vrp->vdev, msg->len);
>  	int err;
>  
>  	dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
> -		msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
> +		virtio32_to_cpu(vrp->vdev, msg->src),
> +		virtio32_to_cpu(vrp->vdev, msg->dst), msg_len,
> +		virtio16_to_cpu(vrp->vdev, msg->flags),
> +		virtio32_to_cpu(vrp->vdev, msg->reserved));
>  #if defined(CONFIG_DYNAMIC_DEBUG)
>  	dynamic_hex_dump("rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
> -			 msg, sizeof(*msg) + msg->len, true);
> +			 msg, sizeof(*msg) + msg_len, true);
>  #endif
>  
>  	/*
> @@ -718,15 +723,15 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
>  	 * the reported payload length.
>  	 */
>  	if (len > vrp->buf_size ||
> -	    msg->len > (len - sizeof(struct rpmsg_hdr))) {
> -		dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg->len);
> +	    msg_len > (len - sizeof(struct rpmsg_hdr))) {
> +		dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg_len);
>  		return -EINVAL;
>  	}
>  
>  	/* use the dst addr to fetch the callback of the appropriate user */
>  	mutex_lock(&vrp->endpoints_lock);
>  
> -	ept = idr_find(&vrp->endpoints, msg->dst);
> +	ept = idr_find(&vrp->endpoints, virtio32_to_cpu(vrp->vdev, msg->dst));
>  
>  	/* let's make sure no one deallocates ept while we use it */
>  	if (ept)
> @@ -739,8 +744,8 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
>  		mutex_lock(&ept->cb_lock);
>  
>  		if (ept->cb)
> -			ept->cb(ept->rpdev, msg->data, msg->len, ept->priv,
> -				msg->src);
> +			ept->cb(ept->rpdev, msg->data, msg_len, ept->priv,
> +				virtio32_to_cpu(vrp->vdev, msg->src));
>  
>  		mutex_unlock(&ept->cb_lock);
>  
> @@ -846,15 +851,15 @@ static int rpmsg_ns_cb(struct rpmsg_device *rpdev, void *data, int len,
>  	/* don't trust the remote processor for null terminating the name */
>  	msg->name[RPMSG_NAME_SIZE - 1] = '\0';
>  
> -	dev_info(dev, "%sing channel %s addr 0x%x\n",
> -		 msg->flags & RPMSG_NS_DESTROY ? "destroy" : "creat",
> -		 msg->name, msg->addr);
> -
>  	strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
>  	chinfo.src = RPMSG_ADDR_ANY;
> -	chinfo.dst = msg->addr;
> +	chinfo.dst = virtio32_to_cpu(vrp->vdev, msg->addr);
> +
> +	dev_info(dev, "%sing channel %s addr 0x%x\n",
> +		 virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY ?
> +		 "destroy" : "creat", msg->name, chinfo.dst);
>  
> -	if (msg->flags & RPMSG_NS_DESTROY) {
> +	if (virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY) {
>  		ret = rpmsg_unregister_device(&vrp->vdev->dev, &chinfo);
>  		if (ret)
>  			dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);


Question: are all these calls taking place *after* feature negotiation
happened? Because virtio32_to_cpu will not DTRT before ...

> -- 
> 2.27.0


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH RESEND v4] rpmsg: virtio: add endianness conversions
  2020-07-27 16:07                         ` Michael S. Tsirkin
@ 2020-07-27 16:09                           ` Mathieu Poirier
  2020-07-28  9:51                             ` Michael S. Tsirkin
  0 siblings, 1 reply; 21+ messages in thread
From: Mathieu Poirier @ 2020-07-27 16:09 UTC (permalink / raw)
  To: Michael S. Tsirkin
  Cc: Guennadi Liakhovetski, Arnaud POULIQUEN, linux-remoteproc,
	Ohad Ben-Cohen, Bjorn Andersson, Pierre-Louis Bossart,
	Liam Girdwood

On Mon, 27 Jul 2020 at 10:07, Michael S. Tsirkin <mst@redhat.com> wrote:
>
> On Tue, Jul 21, 2020 at 10:56:38AM +0200, Guennadi Liakhovetski wrote:
> > According to the VirtIO 1.0 spec data, sent over virtual queues must
> > be in little-endian format. Update the RPMsg VirtIO implementation
> > to enforce that but let legacy configurations continue use native
> > endianness.
> >
> > Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
> > Reviewed-by: Mathieu Poirier <mathieu.poirier@linaro.org>
> > Tested-by: Arnaud Pouliquen <arnaud.pouliquen@st.com>
>
> > ---
> >
> > Ping: 2 weeks since the original submission. Also added back the updated
> > "reviewed-by" tag.
> >
> > v4: fix a left-over use of an LE value in calculation - thanks to Arnaud for
> > catching!
> >
> > v3: use the virtio_byteorder.h header
> >
> > v2: Following suggestions from Michael and Mathieu switch to using virtio16/32
> > types and conversion functions.
> >
> >  drivers/rpmsg/virtio_rpmsg_bus.c | 63 +++++++++++++++++---------------
> >  1 file changed, 34 insertions(+), 29 deletions(-)
> >
> > diff --git a/drivers/rpmsg/virtio_rpmsg_bus.c b/drivers/rpmsg/virtio_rpmsg_bus.c
> > index 07d4f3374098..9006fc7f73d0 100644
> > --- a/drivers/rpmsg/virtio_rpmsg_bus.c
> > +++ b/drivers/rpmsg/virtio_rpmsg_bus.c
> > @@ -23,6 +23,7 @@
> >  #include <linux/slab.h>
> >  #include <linux/sched.h>
> >  #include <linux/virtio.h>
> > +#include <linux/virtio_byteorder.h>
> >  #include <linux/virtio_ids.h>
> >  #include <linux/virtio_config.h>
> >  #include <linux/wait.h>
> > @@ -84,11 +85,11 @@ struct virtproc_info {
> >   * Every message sent(/received) on the rpmsg bus begins with this header.
> >   */
> >  struct rpmsg_hdr {
> > -     u32 src;
> > -     u32 dst;
> > -     u32 reserved;
> > -     u16 len;
> > -     u16 flags;
> > +     __virtio32 src;
> > +     __virtio32 dst;
> > +     __virtio32 reserved;
> > +     __virtio16 len;
> > +     __virtio16 flags;
> >       u8 data[];
> >  } __packed;
> >
> > @@ -106,8 +107,8 @@ struct rpmsg_hdr {
> >   */
> >  struct rpmsg_ns_msg {
> >       char name[RPMSG_NAME_SIZE];
> > -     u32 addr;
> > -     u32 flags;
> > +     __virtio32 addr;
> > +     __virtio32 flags;
> >  } __packed;
> >
> >  /**
> > @@ -335,8 +336,8 @@ static int virtio_rpmsg_announce_create(struct rpmsg_device *rpdev)
> >               struct rpmsg_ns_msg nsm;
> >
> >               strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
> > -             nsm.addr = rpdev->ept->addr;
> > -             nsm.flags = RPMSG_NS_CREATE;
> > +             nsm.addr = cpu_to_virtio32(vrp->vdev, rpdev->ept->addr);
> > +             nsm.flags = cpu_to_virtio32(vrp->vdev, RPMSG_NS_CREATE);
> >
> >               err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
> >               if (err)
> > @@ -359,8 +360,8 @@ static int virtio_rpmsg_announce_destroy(struct rpmsg_device *rpdev)
> >               struct rpmsg_ns_msg nsm;
> >
> >               strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
> > -             nsm.addr = rpdev->ept->addr;
> > -             nsm.flags = RPMSG_NS_DESTROY;
> > +             nsm.addr = cpu_to_virtio32(vrp->vdev, rpdev->ept->addr);
> > +             nsm.flags = cpu_to_virtio32(vrp->vdev, RPMSG_NS_DESTROY);
> >
> >               err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
> >               if (err)
> > @@ -612,18 +613,18 @@ static int rpmsg_send_offchannel_raw(struct rpmsg_device *rpdev,
> >               }
> >       }
> >
> > -     msg->len = len;
> > +     msg->len = cpu_to_virtio16(vrp->vdev, len);
> >       msg->flags = 0;
> > -     msg->src = src;
> > -     msg->dst = dst;
> > +     msg->src = cpu_to_virtio32(vrp->vdev, src);
> > +     msg->dst = cpu_to_virtio32(vrp->vdev, dst);
> >       msg->reserved = 0;
> >       memcpy(msg->data, data, len);
> >
> >       dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
> > -             msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
> > +             src, dst, len, msg->flags, msg->reserved);
> >  #if defined(CONFIG_DYNAMIC_DEBUG)
> >       dynamic_hex_dump("rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
> > -                      msg, sizeof(*msg) + msg->len, true);
> > +                      msg, sizeof(*msg) + len, true);
> >  #endif
> >
> >       rpmsg_sg_init(&sg, msg, sizeof(*msg) + len);
> > @@ -704,13 +705,17 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
> >  {
> >       struct rpmsg_endpoint *ept;
> >       struct scatterlist sg;
> > +     unsigned int msg_len = virtio16_to_cpu(vrp->vdev, msg->len);
> >       int err;
> >
> >       dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
> > -             msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
> > +             virtio32_to_cpu(vrp->vdev, msg->src),
> > +             virtio32_to_cpu(vrp->vdev, msg->dst), msg_len,
> > +             virtio16_to_cpu(vrp->vdev, msg->flags),
> > +             virtio32_to_cpu(vrp->vdev, msg->reserved));
> >  #if defined(CONFIG_DYNAMIC_DEBUG)
> >       dynamic_hex_dump("rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
> > -                      msg, sizeof(*msg) + msg->len, true);
> > +                      msg, sizeof(*msg) + msg_len, true);
> >  #endif
> >
> >       /*
> > @@ -718,15 +723,15 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
> >        * the reported payload length.
> >        */
> >       if (len > vrp->buf_size ||
> > -         msg->len > (len - sizeof(struct rpmsg_hdr))) {
> > -             dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg->len);
> > +         msg_len > (len - sizeof(struct rpmsg_hdr))) {
> > +             dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg_len);
> >               return -EINVAL;
> >       }
> >
> >       /* use the dst addr to fetch the callback of the appropriate user */
> >       mutex_lock(&vrp->endpoints_lock);
> >
> > -     ept = idr_find(&vrp->endpoints, msg->dst);
> > +     ept = idr_find(&vrp->endpoints, virtio32_to_cpu(vrp->vdev, msg->dst));
> >
> >       /* let's make sure no one deallocates ept while we use it */
> >       if (ept)
> > @@ -739,8 +744,8 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
> >               mutex_lock(&ept->cb_lock);
> >
> >               if (ept->cb)
> > -                     ept->cb(ept->rpdev, msg->data, msg->len, ept->priv,
> > -                             msg->src);
> > +                     ept->cb(ept->rpdev, msg->data, msg_len, ept->priv,
> > +                             virtio32_to_cpu(vrp->vdev, msg->src));
> >
> >               mutex_unlock(&ept->cb_lock);
> >
> > @@ -846,15 +851,15 @@ static int rpmsg_ns_cb(struct rpmsg_device *rpdev, void *data, int len,
> >       /* don't trust the remote processor for null terminating the name */
> >       msg->name[RPMSG_NAME_SIZE - 1] = '\0';
> >
> > -     dev_info(dev, "%sing channel %s addr 0x%x\n",
> > -              msg->flags & RPMSG_NS_DESTROY ? "destroy" : "creat",
> > -              msg->name, msg->addr);
> > -
> >       strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
> >       chinfo.src = RPMSG_ADDR_ANY;
> > -     chinfo.dst = msg->addr;
> > +     chinfo.dst = virtio32_to_cpu(vrp->vdev, msg->addr);
> > +
> > +     dev_info(dev, "%sing channel %s addr 0x%x\n",
> > +              virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY ?
> > +              "destroy" : "creat", msg->name, chinfo.dst);
> >
> > -     if (msg->flags & RPMSG_NS_DESTROY) {
> > +     if (virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY) {
> >               ret = rpmsg_unregister_device(&vrp->vdev->dev, &chinfo);
> >               if (ret)
> >                       dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
>
>
> Question: are all these calls taking place *after* feature negotiation
> happened? Because virtio32_to_cpu will not DTRT before ...

After

>
> > --
> > 2.27.0
>

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH RESEND v4] rpmsg: virtio: add endianness conversions
  2020-07-27 16:09                           ` Mathieu Poirier
@ 2020-07-28  9:51                             ` Michael S. Tsirkin
  0 siblings, 0 replies; 21+ messages in thread
From: Michael S. Tsirkin @ 2020-07-28  9:51 UTC (permalink / raw)
  To: Mathieu Poirier
  Cc: Guennadi Liakhovetski, Arnaud POULIQUEN, linux-remoteproc,
	Ohad Ben-Cohen, Bjorn Andersson, Pierre-Louis Bossart,
	Liam Girdwood

On Mon, Jul 27, 2020 at 10:09:43AM -0600, Mathieu Poirier wrote:
> On Mon, 27 Jul 2020 at 10:07, Michael S. Tsirkin <mst@redhat.com> wrote:
> >
> > On Tue, Jul 21, 2020 at 10:56:38AM +0200, Guennadi Liakhovetski wrote:
> > > According to the VirtIO 1.0 spec data, sent over virtual queues must
> > > be in little-endian format. Update the RPMsg VirtIO implementation
> > > to enforce that but let legacy configurations continue use native
> > > endianness.
> > >
> > > Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
> > > Reviewed-by: Mathieu Poirier <mathieu.poirier@linaro.org>
> > > Tested-by: Arnaud Pouliquen <arnaud.pouliquen@st.com>

Acked-by: Michael S. Tsirkin <mst@redhat.com>

Pls feel free to merge.


> > > ---
> > >
> > > Ping: 2 weeks since the original submission. Also added back the updated
> > > "reviewed-by" tag.
> > >
> > > v4: fix a left-over use of an LE value in calculation - thanks to Arnaud for
> > > catching!
> > >
> > > v3: use the virtio_byteorder.h header
> > >
> > > v2: Following suggestions from Michael and Mathieu switch to using virtio16/32
> > > types and conversion functions.
> > >
> > >  drivers/rpmsg/virtio_rpmsg_bus.c | 63 +++++++++++++++++---------------
> > >  1 file changed, 34 insertions(+), 29 deletions(-)
> > >
> > > diff --git a/drivers/rpmsg/virtio_rpmsg_bus.c b/drivers/rpmsg/virtio_rpmsg_bus.c
> > > index 07d4f3374098..9006fc7f73d0 100644
> > > --- a/drivers/rpmsg/virtio_rpmsg_bus.c
> > > +++ b/drivers/rpmsg/virtio_rpmsg_bus.c
> > > @@ -23,6 +23,7 @@
> > >  #include <linux/slab.h>
> > >  #include <linux/sched.h>
> > >  #include <linux/virtio.h>
> > > +#include <linux/virtio_byteorder.h>
> > >  #include <linux/virtio_ids.h>
> > >  #include <linux/virtio_config.h>
> > >  #include <linux/wait.h>
> > > @@ -84,11 +85,11 @@ struct virtproc_info {
> > >   * Every message sent(/received) on the rpmsg bus begins with this header.
> > >   */
> > >  struct rpmsg_hdr {
> > > -     u32 src;
> > > -     u32 dst;
> > > -     u32 reserved;
> > > -     u16 len;
> > > -     u16 flags;
> > > +     __virtio32 src;
> > > +     __virtio32 dst;
> > > +     __virtio32 reserved;
> > > +     __virtio16 len;
> > > +     __virtio16 flags;
> > >       u8 data[];
> > >  } __packed;
> > >
> > > @@ -106,8 +107,8 @@ struct rpmsg_hdr {
> > >   */
> > >  struct rpmsg_ns_msg {
> > >       char name[RPMSG_NAME_SIZE];
> > > -     u32 addr;
> > > -     u32 flags;
> > > +     __virtio32 addr;
> > > +     __virtio32 flags;
> > >  } __packed;
> > >
> > >  /**
> > > @@ -335,8 +336,8 @@ static int virtio_rpmsg_announce_create(struct rpmsg_device *rpdev)
> > >               struct rpmsg_ns_msg nsm;
> > >
> > >               strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
> > > -             nsm.addr = rpdev->ept->addr;
> > > -             nsm.flags = RPMSG_NS_CREATE;
> > > +             nsm.addr = cpu_to_virtio32(vrp->vdev, rpdev->ept->addr);
> > > +             nsm.flags = cpu_to_virtio32(vrp->vdev, RPMSG_NS_CREATE);
> > >
> > >               err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
> > >               if (err)
> > > @@ -359,8 +360,8 @@ static int virtio_rpmsg_announce_destroy(struct rpmsg_device *rpdev)
> > >               struct rpmsg_ns_msg nsm;
> > >
> > >               strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
> > > -             nsm.addr = rpdev->ept->addr;
> > > -             nsm.flags = RPMSG_NS_DESTROY;
> > > +             nsm.addr = cpu_to_virtio32(vrp->vdev, rpdev->ept->addr);
> > > +             nsm.flags = cpu_to_virtio32(vrp->vdev, RPMSG_NS_DESTROY);
> > >
> > >               err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
> > >               if (err)
> > > @@ -612,18 +613,18 @@ static int rpmsg_send_offchannel_raw(struct rpmsg_device *rpdev,
> > >               }
> > >       }
> > >
> > > -     msg->len = len;
> > > +     msg->len = cpu_to_virtio16(vrp->vdev, len);
> > >       msg->flags = 0;
> > > -     msg->src = src;
> > > -     msg->dst = dst;
> > > +     msg->src = cpu_to_virtio32(vrp->vdev, src);
> > > +     msg->dst = cpu_to_virtio32(vrp->vdev, dst);
> > >       msg->reserved = 0;
> > >       memcpy(msg->data, data, len);
> > >
> > >       dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
> > > -             msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
> > > +             src, dst, len, msg->flags, msg->reserved);
> > >  #if defined(CONFIG_DYNAMIC_DEBUG)
> > >       dynamic_hex_dump("rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
> > > -                      msg, sizeof(*msg) + msg->len, true);
> > > +                      msg, sizeof(*msg) + len, true);
> > >  #endif
> > >
> > >       rpmsg_sg_init(&sg, msg, sizeof(*msg) + len);
> > > @@ -704,13 +705,17 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
> > >  {
> > >       struct rpmsg_endpoint *ept;
> > >       struct scatterlist sg;
> > > +     unsigned int msg_len = virtio16_to_cpu(vrp->vdev, msg->len);
> > >       int err;
> > >
> > >       dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
> > > -             msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
> > > +             virtio32_to_cpu(vrp->vdev, msg->src),
> > > +             virtio32_to_cpu(vrp->vdev, msg->dst), msg_len,
> > > +             virtio16_to_cpu(vrp->vdev, msg->flags),
> > > +             virtio32_to_cpu(vrp->vdev, msg->reserved));
> > >  #if defined(CONFIG_DYNAMIC_DEBUG)
> > >       dynamic_hex_dump("rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
> > > -                      msg, sizeof(*msg) + msg->len, true);
> > > +                      msg, sizeof(*msg) + msg_len, true);
> > >  #endif
> > >
> > >       /*
> > > @@ -718,15 +723,15 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
> > >        * the reported payload length.
> > >        */
> > >       if (len > vrp->buf_size ||
> > > -         msg->len > (len - sizeof(struct rpmsg_hdr))) {
> > > -             dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg->len);
> > > +         msg_len > (len - sizeof(struct rpmsg_hdr))) {
> > > +             dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg_len);
> > >               return -EINVAL;
> > >       }
> > >
> > >       /* use the dst addr to fetch the callback of the appropriate user */
> > >       mutex_lock(&vrp->endpoints_lock);
> > >
> > > -     ept = idr_find(&vrp->endpoints, msg->dst);
> > > +     ept = idr_find(&vrp->endpoints, virtio32_to_cpu(vrp->vdev, msg->dst));
> > >
> > >       /* let's make sure no one deallocates ept while we use it */
> > >       if (ept)
> > > @@ -739,8 +744,8 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
> > >               mutex_lock(&ept->cb_lock);
> > >
> > >               if (ept->cb)
> > > -                     ept->cb(ept->rpdev, msg->data, msg->len, ept->priv,
> > > -                             msg->src);
> > > +                     ept->cb(ept->rpdev, msg->data, msg_len, ept->priv,
> > > +                             virtio32_to_cpu(vrp->vdev, msg->src));
> > >
> > >               mutex_unlock(&ept->cb_lock);
> > >
> > > @@ -846,15 +851,15 @@ static int rpmsg_ns_cb(struct rpmsg_device *rpdev, void *data, int len,
> > >       /* don't trust the remote processor for null terminating the name */
> > >       msg->name[RPMSG_NAME_SIZE - 1] = '\0';
> > >
> > > -     dev_info(dev, "%sing channel %s addr 0x%x\n",
> > > -              msg->flags & RPMSG_NS_DESTROY ? "destroy" : "creat",
> > > -              msg->name, msg->addr);
> > > -
> > >       strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
> > >       chinfo.src = RPMSG_ADDR_ANY;
> > > -     chinfo.dst = msg->addr;
> > > +     chinfo.dst = virtio32_to_cpu(vrp->vdev, msg->addr);
> > > +
> > > +     dev_info(dev, "%sing channel %s addr 0x%x\n",
> > > +              virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY ?
> > > +              "destroy" : "creat", msg->name, chinfo.dst);
> > >
> > > -     if (msg->flags & RPMSG_NS_DESTROY) {
> > > +     if (virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY) {
> > >               ret = rpmsg_unregister_device(&vrp->vdev->dev, &chinfo);
> > >               if (ret)
> > >                       dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
> >
> >
> > Question: are all these calls taking place *after* feature negotiation
> > happened? Because virtio32_to_cpu will not DTRT before ...
> 
> After
> 
> >
> > > --
> > > 2.27.0
> >


^ permalink raw reply	[flat|nested] 21+ messages in thread

end of thread, back to index

Thread overview: 21+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-08 12:39 [PATCH] rpmsg: virtio: add endianness conversions Guennadi Liakhovetski
2020-06-08 13:13 ` Michael S. Tsirkin
2020-06-15 20:47 ` Mathieu Poirier
2020-06-16  6:14   ` Guennadi Liakhovetski
2020-06-16  7:30     ` [PATCH v2] " Guennadi Liakhovetski
2020-06-30 17:50       ` Mathieu Poirier
2020-07-01 10:00         ` Michael S. Tsirkin
2020-07-06 12:47           ` Guennadi Liakhovetski
2020-07-06 12:56             ` Guennadi Liakhovetski
2020-07-06 13:38               ` [PATCH v3] " Guennadi Liakhovetski
2020-07-07 15:11                 ` Arnaud POULIQUEN
2020-07-07 15:34                   ` [PATCH v4] " Guennadi Liakhovetski
2020-07-07 15:38                     ` Mathieu Poirier
2020-07-10  6:20                       ` Guennadi Liakhovetski
2020-07-21  8:56                       ` [PATCH RESEND " Guennadi Liakhovetski
2020-07-21 10:04                         ` Michael S. Tsirkin
2020-07-22  3:39                         ` Bjorn Andersson
2020-07-22  4:00                         ` patchwork-bot+linux-remoteproc
2020-07-27 16:07                         ` Michael S. Tsirkin
2020-07-27 16:09                           ` Mathieu Poirier
2020-07-28  9:51                             ` Michael S. Tsirkin

Linux-remoteproc Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-remoteproc/0 linux-remoteproc/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-remoteproc linux-remoteproc/ https://lore.kernel.org/linux-remoteproc \
		linux-remoteproc@vger.kernel.org
	public-inbox-index linux-remoteproc

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-remoteproc


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git