Skip to contentSkip to conversation

vhost: fix external message handlers

PATCH Superseded David Marchand 4 participants 9 messages Mar 7, 2022 – Mar 8, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vhost: fix external message handlers

Following a rework, external message handlers were receiving a pointer to a vhost_user message (as stated in the API), but lost the ability to interact with fds attached to the message.
Restore the original layout and put a build check and reminders.

Bugzilla ID: 953

Fixes 5e0099dc709e ("vhost: remove payload size limitation")

---
This patch is untested, but sending quickly to get feedback from the reporter and comments from author and maintainers.

---
 lib/vhost/vhost_user.c | 8 ++++----
 lib/vhost/vhost_user.h | 7 +++++--
 2 files changed, 9 insertions(+), 6 deletions(-)
lib/vhost/vhost_user.c +4 -4
index 723c6890c3..589b950458 100644
@@ -3023,8 +3023,8 @@ vhost_user_msg_handler(int vid, int fd)
 
 	handled = false;
 	if (dev->extern_ops.pre_msg_handle) {
-		ret = (*dev->extern_ops.pre_msg_handle)(dev->vid,
-				(void *)&ctx.msg);
+		RTE_BUILD_BUG_ON(offsetof(struct vhu_msg_context, msg) != 0);
+		ret = (*dev->extern_ops.pre_msg_handle)(dev->vid, &ctx);
 		switch (ret) {
 		case RTE_VHOST_MSG_RESULT_REPLY:
 			send_vhost_reply(dev, fd, &ctx);
@@ -3069,8 +3069,8 @@ vhost_user_msg_handler(int vid, int fd)
 skip_to_post_handle:
 	if (ret != RTE_VHOST_MSG_RESULT_ERR &&
 			dev->extern_ops.post_msg_handle) {
-		ret = (*dev->extern_ops.post_msg_handle)(dev->vid,
-				(void *)&ctx.msg);
+		RTE_BUILD_BUG_ON(offsetof(struct vhu_msg_context, msg) != 0);
+		ret = (*dev->extern_ops.post_msg_handle)(dev->vid, &ctx);
 		switch (ret) {
 		case RTE_VHOST_MSG_RESULT_REPLY:
 			send_vhost_reply(dev, fd, &ctx);
lib/vhost/vhost_user.h +5 -2
index be53669f3b..555f89c97a 100644
@@ -152,10 +152,13 @@ typedef struct VhostUserMsg {
 	/* Nothing should be added after the payload */
 } __rte_packed VhostUserMsg;
 
-struct vhu_msg_context {
+/* Note: this structure and VhostUserMsg can't be changed carelessly as
+ * external message handlers rely on them.
+ */
+__rte_packed struct vhu_msg_context {
+	VhostUserMsg msg;
 	int fds[VHOST_MEMORY_MAX_NREGIONS];
 	int fd_num;
-	VhostUserMsg msg;
 };
 
 #define VHOST_USER_HDR_SIZE offsetof(VhostUserMsg, payload.u64)
Signature · 1 line
2.23.0
Reviewed-by on v1 1/1
#1 lore raw

Hi,

Quoted 55 lines · David Marchand

Following a rework, external message handlers were receiving a pointer to a vhost_user message (as stated in the API), but lost the ability to interact with fds attached to the message.
Restore the original layout and put a build check and reminders.

Bugzilla ID: 953

Fixes 5e0099dc709e ("vhost: remove payload size limitation")

---
This patch is untested, but sending quickly to get feedback from the reporter and comments from author and maintainers.

---
  lib/vhost/vhost_user.c | 8 ++++----
  lib/vhost/vhost_user.h | 7 +++++--
  2 files changed, 9 insertions(+), 6 deletions(-)
lib/vhost/vhost_user.c +4 -4
index 723c6890c3..589b950458 100644
@@ -3023,8 +3023,8 @@ vhost_user_msg_handler(int vid, int fd)
 
  	handled = false;
  	if (dev->extern_ops.pre_msg_handle) {
-		ret = (*dev->extern_ops.pre_msg_handle)(dev->vid,
-				(void *)&ctx.msg);
+		RTE_BUILD_BUG_ON(offsetof(struct vhu_msg_context, msg) != 0);
+		ret = (*dev->extern_ops.pre_msg_handle)(dev->vid, &ctx);
  		switch (ret) {
  		case RTE_VHOST_MSG_RESULT_REPLY:
  			send_vhost_reply(dev, fd, &ctx);
@@ -3069,8 +3069,8 @@ vhost_user_msg_handler(int vid, int fd)
  skip_to_post_handle:
  	if (ret != RTE_VHOST_MSG_RESULT_ERR &&
  			dev->extern_ops.post_msg_handle) {
-		ret = (*dev->extern_ops.post_msg_handle)(dev->vid,
-				(void *)&ctx.msg);
+		RTE_BUILD_BUG_ON(offsetof(struct vhu_msg_context, msg) != 0);
+		ret = (*dev->extern_ops.post_msg_handle)(dev->vid, &ctx);
  		switch (ret) {
  		case RTE_VHOST_MSG_RESULT_REPLY:
  			send_vhost_reply(dev, fd, &ctx);
lib/vhost/vhost_user.h +1 -1
index be53669f3b..555f89c97a 100644
@@ -152,10 +152,13 @@ typedef struct VhostUserMsg {
  	/* Nothing should be added after the payload */
  } __rte_packed VhostUserMsg;
 
-struct vhu_msg_context {
+/* Note: this structure and VhostUserMsg can't be changed carelessly as

lib/vhost/vhost_user.h typedef struct VhostUserMsg { · line 164

Quoted 10 lines
lib/vhost/vhost_user.h +4 -1
+ * external message handlers rely on them.
+ */
+__rte_packed struct vhu_msg_context {
+	VhostUserMsg msg;
  	int fds[VHOST_MEMORY_MAX_NREGIONS];
  	int fd_num;
-	VhostUserMsg msg;
  };
 
  #define VHOST_USER_HDR_SIZE offsetof(VhostUserMsg, payload.u64)

We should revisit the callbacks prototype when we'll be allowed to break API, passing the message pointer as void * is definitely not a good idea.

In the mean time, I agree with your fix:

We may also add Reported-by tag when applying.

Thanks!
Maxime

#2 lore raw
Quoted 65 lines · Maxime Coquelin
Quoted 64 lines · David Marchand

Following a rework, external message handlers were receiving a pointer to a vhost_user message (as stated in the API), but lost the ability to interact with fds attached to the message.
Restore the original layout and put a build check and reminders.

Bugzilla ID: 953

Fixes 5e0099dc709e ("vhost: remove payload size limitation")

---
This patch is untested, but sending quickly to get feedback from the reporter and comments from author and maintainers.

---
  lib/vhost/vhost_user.c | 8 ++++----
  lib/vhost/vhost_user.h | 7 +++++--
  2 files changed, 9 insertions(+), 6 deletions(-)
lib/vhost/vhost_user.c +4 -4
index 723c6890c3..589b950458 100644
@@ -3023,8 +3023,8 @@ vhost_user_msg_handler(int vid, int fd)
 
      handled = false;
      if (dev->extern_ops.pre_msg_handle) {
-             ret = (*dev->extern_ops.pre_msg_handle)(dev->vid,
-                             (void *)&ctx.msg);
+             RTE_BUILD_BUG_ON(offsetof(struct vhu_msg_context, msg) != 0);
+             ret = (*dev->extern_ops.pre_msg_handle)(dev->vid, &ctx);
              switch (ret) {
              case RTE_VHOST_MSG_RESULT_REPLY:
                      send_vhost_reply(dev, fd, &ctx);
@@ -3069,8 +3069,8 @@ vhost_user_msg_handler(int vid, int fd)
  skip_to_post_handle:
      if (ret != RTE_VHOST_MSG_RESULT_ERR &&
                      dev->extern_ops.post_msg_handle) {
-             ret = (*dev->extern_ops.post_msg_handle)(dev->vid,
-                             (void *)&ctx.msg);
+             RTE_BUILD_BUG_ON(offsetof(struct vhu_msg_context, msg) != 0);
+             ret = (*dev->extern_ops.post_msg_handle)(dev->vid, &ctx);
              switch (ret) {
              case RTE_VHOST_MSG_RESULT_REPLY:
                      send_vhost_reply(dev, fd, &ctx);
lib/vhost/vhost_user.h +5 -2
index be53669f3b..555f89c97a 100644
@@ -152,10 +152,13 @@ typedef struct VhostUserMsg {
      /* Nothing should be added after the payload */
  } __rte_packed VhostUserMsg;
 
-struct vhu_msg_context {
+/* Note: this structure and VhostUserMsg can't be changed carelessly as
+ * external message handlers rely on them.
+ */
+__rte_packed struct vhu_msg_context {
+     VhostUserMsg msg;
      int fds[VHOST_MEMORY_MAX_NREGIONS];
      int fd_num;
-     VhostUserMsg msg;
  };
 
Quoted 5 lines

lib/vhost/vhost_user.h typedef struct VhostUserMsg { · line 164

Quoted 1 line
lib/vhost/vhost_user.h +0 -0
  #define VHOST_USER_HDR_SIZE offsetof(VhostUserMsg, payload.u64)

We should revisit the callbacks prototype when we'll be allowed to break API, passing the message pointer as void * is definitely not a good idea.

Indeed.

Quoted 6 lines

In the mean time, I agree with your fix:

We may also add Reported-by tag when applying.

There is an issue with clang, I'll send a v2.

Signature · 1 line
David Marchand
#3 lore raw
Quoted 76 lines · David Marchand
Quoted 65 lines · Maxime Coquelin
Quoted 64 lines · David Marchand

Following a rework, external message handlers were receiving a pointer to a vhost_user message (as stated in the API), but lost the ability to interact with fds attached to the message.
Restore the original layout and put a build check and reminders.

Bugzilla ID: 953

Fixes 5e0099dc709e ("vhost: remove payload size limitation")

---
This patch is untested, but sending quickly to get feedback from the reporter and comments from author and maintainers.

---
   lib/vhost/vhost_user.c | 8 ++++----
   lib/vhost/vhost_user.h | 7 +++++--
   2 files changed, 9 insertions(+), 6 deletions(-)
lib/vhost/vhost_user.c +4 -4
index 723c6890c3..589b950458 100644
@@ -3023,8 +3023,8 @@ vhost_user_msg_handler(int vid, int fd)
 
       handled = false;
       if (dev->extern_ops.pre_msg_handle) {
-             ret = (*dev->extern_ops.pre_msg_handle)(dev->vid,
-                             (void *)&ctx.msg);
+             RTE_BUILD_BUG_ON(offsetof(struct vhu_msg_context, msg) != 0);
+             ret = (*dev->extern_ops.pre_msg_handle)(dev->vid, &ctx);
               switch (ret) {
               case RTE_VHOST_MSG_RESULT_REPLY:
                       send_vhost_reply(dev, fd, &ctx);
@@ -3069,8 +3069,8 @@ vhost_user_msg_handler(int vid, int fd)
   skip_to_post_handle:
       if (ret != RTE_VHOST_MSG_RESULT_ERR &&
                       dev->extern_ops.post_msg_handle) {
-             ret = (*dev->extern_ops.post_msg_handle)(dev->vid,
-                             (void *)&ctx.msg);
+             RTE_BUILD_BUG_ON(offsetof(struct vhu_msg_context, msg) != 0);
+             ret = (*dev->extern_ops.post_msg_handle)(dev->vid, &ctx);
               switch (ret) {
               case RTE_VHOST_MSG_RESULT_REPLY:
                       send_vhost_reply(dev, fd, &ctx);
lib/vhost/vhost_user.h +5 -2
index be53669f3b..555f89c97a 100644
@@ -152,10 +152,13 @@ typedef struct VhostUserMsg {
       /* Nothing should be added after the payload */
   } __rte_packed VhostUserMsg;
 
-struct vhu_msg_context {
+/* Note: this structure and VhostUserMsg can't be changed carelessly as
+ * external message handlers rely on them.
+ */
+__rte_packed struct vhu_msg_context {
+     VhostUserMsg msg;
       int fds[VHOST_MEMORY_MAX_NREGIONS];
       int fd_num;
-     VhostUserMsg msg;
   };
 
Quoted 5 lines

lib/vhost/vhost_user.h typedef struct VhostUserMsg { · line 164

Quoted 1 line
lib/vhost/vhost_user.h +0 -0
   #define VHOST_USER_HDR_SIZE offsetof(VhostUserMsg, payload.u64)

We should revisit the callbacks prototype when we'll be allowed to break API, passing the message pointer as void * is definitely not a good idea.

Indeed.

Quoted 1 line
Quoted 8 lines
Quoted 5 lines

In the mean time, I agree with your fix:

We may also add Reported-by tag when applying.

There is an issue with clang, I'll send a v2.

Ok.
Fan, can you have a try and confirm it fixes the issue on your side?

Thanks,
Maxime

#4 lore raw

PATCH v2 1/1 vhost: fix external message handlers

In reply to David Marchand

Following a rework, external message handlers were receiving a pointer to a vhost_user message (as stated in the API), but lost the ability to interact with fds attached to the message.
Restore the original layout and put a build check and reminders.

Bugzilla ID: 953

Fixes 5e0099dc709e ("vhost: remove payload size limitation")

---
Changes since v1:
- fixed build with clang,

---
 lib/vhost/vhost_user.c | 8 ++++----
 lib/vhost/vhost_user.h | 7 +++++--
 2 files changed, 9 insertions(+), 6 deletions(-)
lib/vhost/vhost_user.c +4 -4
index 723c6890c3..589b950458 100644
@@ -3023,8 +3023,8 @@ vhost_user_msg_handler(int vid, int fd)
 
 	handled = false;
 	if (dev->extern_ops.pre_msg_handle) {
-		ret = (*dev->extern_ops.pre_msg_handle)(dev->vid,
-				(void *)&ctx.msg);
+		RTE_BUILD_BUG_ON(offsetof(struct vhu_msg_context, msg) != 0);
+		ret = (*dev->extern_ops.pre_msg_handle)(dev->vid, &ctx);
 		switch (ret) {
 		case RTE_VHOST_MSG_RESULT_REPLY:
 			send_vhost_reply(dev, fd, &ctx);
@@ -3069,8 +3069,8 @@ vhost_user_msg_handler(int vid, int fd)
 skip_to_post_handle:
 	if (ret != RTE_VHOST_MSG_RESULT_ERR &&
 			dev->extern_ops.post_msg_handle) {
-		ret = (*dev->extern_ops.post_msg_handle)(dev->vid,
-				(void *)&ctx.msg);
+		RTE_BUILD_BUG_ON(offsetof(struct vhu_msg_context, msg) != 0);
+		ret = (*dev->extern_ops.post_msg_handle)(dev->vid, &ctx);
 		switch (ret) {
 		case RTE_VHOST_MSG_RESULT_REPLY:
 			send_vhost_reply(dev, fd, &ctx);
lib/vhost/vhost_user.h +5 -2
index be53669f3b..c946cc2ef4 100644
@@ -152,10 +152,13 @@ typedef struct VhostUserMsg {
 	/* Nothing should be added after the payload */
 } __rte_packed VhostUserMsg;
 
-struct vhu_msg_context {
+/* Note: this structure and VhostUserMsg can't be changed carelessly as
+ * external message handlers rely on them.
+ */
+struct __rte_packed vhu_msg_context {
+	VhostUserMsg msg;
 	int fds[VHOST_MEMORY_MAX_NREGIONS];
 	int fd_num;
-	VhostUserMsg msg;
 };
 
 #define VHOST_USER_HDR_SIZE offsetof(VhostUserMsg, payload.u64)
Signature · 1 line
2.23.0
Reviewed-by on v2 1/1
#5 lore raw
Quoted 55 lines · David Marchand

Following a rework, external message handlers were receiving a pointer to a vhost_user message (as stated in the API), but lost the ability to interact with fds attached to the message.
Restore the original layout and put a build check and reminders.

Bugzilla ID: 953

Fixes 5e0099dc709e ("vhost: remove payload size limitation")

---
Changes since v1:
- fixed build with clang,

---
  lib/vhost/vhost_user.c | 8 ++++----
  lib/vhost/vhost_user.h | 7 +++++--
  2 files changed, 9 insertions(+), 6 deletions(-)
lib/vhost/vhost_user.c +4 -4
index 723c6890c3..589b950458 100644
@@ -3023,8 +3023,8 @@ vhost_user_msg_handler(int vid, int fd)
 
  	handled = false;
  	if (dev->extern_ops.pre_msg_handle) {
-		ret = (*dev->extern_ops.pre_msg_handle)(dev->vid,
-				(void *)&ctx.msg);
+		RTE_BUILD_BUG_ON(offsetof(struct vhu_msg_context, msg) != 0);
+		ret = (*dev->extern_ops.pre_msg_handle)(dev->vid, &ctx);
  		switch (ret) {
  		case RTE_VHOST_MSG_RESULT_REPLY:
  			send_vhost_reply(dev, fd, &ctx);
@@ -3069,8 +3069,8 @@ vhost_user_msg_handler(int vid, int fd)
  skip_to_post_handle:
  	if (ret != RTE_VHOST_MSG_RESULT_ERR &&
  			dev->extern_ops.post_msg_handle) {
-		ret = (*dev->extern_ops.post_msg_handle)(dev->vid,
-				(void *)&ctx.msg);
+		RTE_BUILD_BUG_ON(offsetof(struct vhu_msg_context, msg) != 0);
+		ret = (*dev->extern_ops.post_msg_handle)(dev->vid, &ctx);
  		switch (ret) {
  		case RTE_VHOST_MSG_RESULT_REPLY:
  			send_vhost_reply(dev, fd, &ctx);
lib/vhost/vhost_user.h +1 -1
index be53669f3b..c946cc2ef4 100644
@@ -152,10 +152,13 @@ typedef struct VhostUserMsg {
  	/* Nothing should be added after the payload */
  } __rte_packed VhostUserMsg;
 
-struct vhu_msg_context {
+/* Note: this structure and VhostUserMsg can't be changed carelessly as

lib/vhost/vhost_user.h typedef struct VhostUserMsg { · line 164

Quoted 10 lines
lib/vhost/vhost_user.h +4 -1
+ * external message handlers rely on them.
+ */
+struct __rte_packed vhu_msg_context {
+	VhostUserMsg msg;
  	int fds[VHOST_MEMORY_MAX_NREGIONS];
  	int fd_num;
-	VhostUserMsg msg;
  };
 
  #define VHOST_USER_HDR_SIZE offsetof(VhostUserMsg, payload.u64)

Thanks,
Maxime

Tested-by Acked-by
#6 lore raw

Hi everyone,

Quoted 8 lines

Following a rework, external message handlers were receiving a pointer to a vhost_user message (as stated in the API), but lost the ability to interact with fds attached to the message.
Restore the original layout and put a build check and reminders.

Bugzilla ID: 953

Fixes 5e0099dc709e ("vhost: remove payload size limitation")
Quoted 7 lines

---
Changes since v1:
- fixed build with clang,

---

Reviewed-by on v2 1/1
#7 lore raw
In reply to David Marchand
Quoted 67 lines · David Marchand

Following a rework, external message handlers were receiving a pointer to a vhost_user message (as stated in the API), but lost the ability to interact with fds attached to the message.
Restore the original layout and put a build check and reminders.

Bugzilla ID: 953

Fixes 5e0099dc709e ("vhost: remove payload size limitation")

---
Changes since v1:
- fixed build with clang,

---
 lib/vhost/vhost_user.c | 8 ++++----
 lib/vhost/vhost_user.h | 7 +++++--
 2 files changed, 9 insertions(+), 6 deletions(-)
lib/vhost/vhost_user.c +4 -4
index 723c6890c3..589b950458 100644
@@ -3023,8 +3023,8 @@ vhost_user_msg_handler(int vid, int fd)
 
        handled = false;
        if (dev->extern_ops.pre_msg_handle) {
-               ret = (*dev->extern_ops.pre_msg_handle)(dev->vid,
-                               (void *)&ctx.msg);
+               RTE_BUILD_BUG_ON(offsetof(struct vhu_msg_context, msg) != 0);
+               ret = (*dev->extern_ops.pre_msg_handle)(dev->vid, &ctx);
                switch (ret) {
                case RTE_VHOST_MSG_RESULT_REPLY:
                        send_vhost_reply(dev, fd, &ctx);
@@ -3069,8 +3069,8 @@ vhost_user_msg_handler(int vid, int fd)
 skip_to_post_handle:
        if (ret != RTE_VHOST_MSG_RESULT_ERR &&
                        dev->extern_ops.post_msg_handle) {
-               ret = (*dev->extern_ops.post_msg_handle)(dev->vid,
-                               (void *)&ctx.msg);
+               RTE_BUILD_BUG_ON(offsetof(struct vhu_msg_context, msg) != 0);
+               ret = (*dev->extern_ops.post_msg_handle)(dev->vid, &ctx);
                switch (ret) {
                case RTE_VHOST_MSG_RESULT_REPLY:
                        send_vhost_reply(dev, fd, &ctx);
lib/vhost/vhost_user.h +5 -3
index be53669f3b..c946cc2ef4 100644
@@ -152,10 +152,13 @@ typedef struct VhostUserMsg {
        /* Nothing should be added after the payload */
 } __rte_packed VhostUserMsg;
 
-struct vhu_msg_context {
+/* Note: this structure and VhostUserMsg can't be changed carelessly as
+ * external message handlers rely on them.
+ */
+struct __rte_packed vhu_msg_context {
+       VhostUserMsg msg;
        int fds[VHOST_MEMORY_MAX_NREGIONS];
        int fd_num;
-       VhostUserMsg msg;
 };
 
 #define VHOST_USER_HDR_SIZE offsetof(VhostUserMsg, payload.u64)
--
Quoted 2 lines

2.23.0

Sorry to have missed that test with virtio-crypto ; I'll create another thread to talk about the VhostUserMsg extension.

Thanks,
Christophe

author Reviewed-by Maxime Coquelin Tested-by Jakub Poczatek Acked-by Jakub Poczatek Reviewed-by Christophe Fontaine on v2 1/1 Applied
#8 lore raw
In reply to David Marchand
Quoted 11 lines · David Marchand

Following a rework, external message handlers were receiving a pointer to a vhost_user message (as stated in the API), but lost the ability to interact with fds attached to the message.
Restore the original layout and put a build check and reminders.

Bugzilla ID: 953

Fixes 5e0099dc709e ("vhost: remove payload size limitation")

Applied, thanks.

Signature · 1 line
David Marchand