Skip to contentSkip to conversation

libvhost-user: handle NOFD flag in call/kick/err better

PATCH In review Johannes Berg 3 participants 6 messages Sep 17, 2019 – Sep 20, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libvhost-user: handle NOFD flag in call/kick/err better

From: Johannes Berg <[email protected]>

The code here is odd, for example will it print out invalid file descriptor numbers that were never sent in the message.

Clean that up a bit so it's actually possible to implement a device that uses polling.

---
 contrib/libvhost-user/libvhost-user.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
contrib/libvhost-user/libvhost-user.c +16 -8
index f1677da21201..17b7833d1f6b 100644
@@ -920,6 +920,7 @@ static bool
 vu_check_queue_msg_file(VuDev *dev, VhostUserMsg *vmsg)
 {
     int index = vmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
+    bool nofd = vmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK;
 
     if (index >= dev->max_queues) {
         vmsg_close_fds(vmsg);
@@ -927,8 +928,12 @@ vu_check_queue_msg_file(VuDev *dev, VhostUserMsg *vmsg)
         return false;
     }
 
-    if (vmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK ||
-        vmsg->fd_num != 1) {
+    if (nofd) {
+        vmsg_close_fds(vmsg);
+        return true;
+    }
+
+    if (vmsg->fd_num != 1) {
         vmsg_close_fds(vmsg);
         vu_panic(dev, "Invalid fds in request: %d", vmsg->request);
         return false;
@@ -1025,6 +1030,7 @@ static bool
 vu_set_vring_kick_exec(VuDev *dev, VhostUserMsg *vmsg)
 {
     int index = vmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
+    bool nofd = vmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK;
 
     DPRINT("u64: 0x%016"PRIx64"\n", vmsg->payload.u64);
 
@@ -1038,8 +1044,8 @@ vu_set_vring_kick_exec(VuDev *dev, VhostUserMsg *vmsg)
         dev->vq[index].kick_fd = -1;
     }
 
-    dev->vq[index].kick_fd = vmsg->fds[0];
-    DPRINT("Got kick_fd: %d for vq: %d\n", vmsg->fds[0], index);
+    dev->vq[index].kick_fd = nofd ? -1 : vmsg->fds[0];
+    DPRINT("Got kick_fd: %d for vq: %d\n", dev->vq[index].kick_fd, index);
 
     dev->vq[index].started = true;
     if (dev->iface->queue_set_started) {
@@ -1116,6 +1122,7 @@ static bool
 vu_set_vring_call_exec(VuDev *dev, VhostUserMsg *vmsg)
 {
     int index = vmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
+    bool nofd = vmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK;
 
     DPRINT("u64: 0x%016"PRIx64"\n", vmsg->payload.u64);
 
@@ -1128,14 +1135,14 @@ vu_set_vring_call_exec(VuDev *dev, VhostUserMsg *vmsg)
         dev->vq[index].call_fd = -1;
     }
 
-    dev->vq[index].call_fd = vmsg->fds[0];
+    dev->vq[index].call_fd = nofd ? -1 : vmsg->fds[0];
 
     /* in case of I/O hang after reconnecting */
-    if (eventfd_write(vmsg->fds[0], 1)) {
+    if (dev->vq[index].call_fd != -1 && eventfd_write(vmsg->fds[0], 1)) {
         return -1;
     }
 
-    DPRINT("Got call_fd: %d for vq: %d\n", vmsg->fds[0], index);
+    DPRINT("Got call_fd: %d for vq: %d\n", dev->vq[index].call_fd, index);
 
     return false;
 }
@@ -1144,6 +1151,7 @@ static bool
 vu_set_vring_err_exec(VuDev *dev, VhostUserMsg *vmsg)
 {
     int index = vmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
+    bool nofd = vmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK;
 
     DPRINT("u64: 0x%016"PRIx64"\n", vmsg->payload.u64);
 
@@ -1156,7 +1164,7 @@ vu_set_vring_err_exec(VuDev *dev, VhostUserMsg *vmsg)
         dev->vq[index].err_fd = -1;
     }
 
-    dev->vq[index].err_fd = vmsg->fds[0];
+    dev->vq[index].err_fd = nofd ? -1 : vmsg->fds[0];
 
     return false;
 }
Signature · 1 line
2.20.1
#1 lore raw
Patchew URL: https://patchew.org/QEMU/[email protected]/

Patchew URL: https://patchew.org/QEMU/[email protected]/

Hi,

This series failed the asan build test. Please find the testing commands and their output below. If you have Docker installed, you can probably reproduce it locally.

=== TEST SCRIPT BEGIN ===
#!/bin/bash
make docker-image-fedora V=1 NETWORK=1
time make docker-test-debug@fedora TARGET_LIST=x86_64-softmmu J=14 NETWORK=1 === TEST SCRIPT END ===

./tests/docker/docker.py --engine auto build qemu:fedora tests/docker/dockerfiles/fedora.docker --add-current-user
Image is up to date.
LD [email protected]
cc: fatal error: no input files
compilation terminated.
make: *** [[email protected]] Error 4

The full log is available at
http://patchew.org/logs/[email protected]/testing.asan/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]

#2 lore raw
In reply to Johannes Berg
Patchew URL: https://patchew.org/QEMU/[email protected]/

Patchew URL: https://patchew.org/QEMU/[email protected]/

Hi,

This series failed the docker-mingw@fedora build test. Please find the testing commands and their output below. If you have Docker installed, you can probably reproduce it locally.

=== TEST SCRIPT BEGIN ===
#! /bin/bash
make docker-image-fedora V=1 NETWORK=1
time make docker-test-mingw@fedora J=14 NETWORK=1
=== TEST SCRIPT END ===

./tests/docker/docker.py --engine auto build qemu:fedora tests/docker/dockerfiles/fedora.docker --add-current-user
Image is up to date.
LD [email protected]
cc: fatal error: no input files
compilation terminated.
make: *** [[email protected]] Error 4

The full log is available at
http://patchew.org/logs/[email protected]/testing.docker-mingw@fedora/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]

#3 lore raw
In reply to Johannes Berg
Quoted 13 lines · Johannes Berg
contrib/libvhost-user/libvhost-user.c +1 -0
index f1677da21201..17b7833d1f6b 100644
@@ -920,6 +920,7 @@ static bool
 vu_check_queue_msg_file(VuDev *dev, VhostUserMsg *vmsg)
 {
     int index = vmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
+    bool nofd = vmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK;
 
     if (index >= dev->max_queues) {
         vmsg_close_fds(vmsg);

contrib/libvhost-user/libvhost-user.c vu_check_queue_msg_file(VuDev *dev, VhostUserMsg *vmsg) · line 934

Quoted 9 lines
contrib/libvhost-user/libvhost-user.c +4 -2
@@ -927,8 +928,12 @@ vu_check_queue_msg_file(VuDev *dev, VhostUserMsg *vmsg)
         return false;
     }
 
-    if (vmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK ||
-        vmsg->fd_num != 1) {
+    if (nofd) {
+        vmsg_close_fds(vmsg);
+        return true;
+    }

With the following change to vmsg_close_fds():

  for (i = 0; i < vmsg->fd_num; i++) {
      close(vmsg->fds[i]);

}
+ for (i = 0; i < sizeof(vmsg->fd_num) / sizeof(vmsg->fd_num[0]); i++) {
+ vmsg->fds[i] = -1;
+ }
+ vmsg->fd_num = 0;

...the message handler functions below can use vmsg->fds[0] (-1) without worrying about NOFD. This makes the code simpler.

#4 lore raw
Quoted 12 lines · Stefan Hajnoczi
Quoted 12 lines

vu_check_queue_msg_file(VuDev *dev, VhostUserMsg *vmsg)

 {
     int index = vmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
contrib/libvhost-user/libvhost-user.c +1 -0
+    bool nofd = vmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK;
 
     if (index >= dev->max_queues) {
         vmsg_close_fds(vmsg);
@@ -927,8 +928,12 @@ vu_check_queue_msg_file(VuDev *dev, VhostUserMsg *vmsg)
         return false;
     }
 
Quoted 6 lines

contrib/libvhost-user/libvhost-user.c vu_check_queue_msg_file(VuDev *dev, VhostUserMsg *vmsg) · line 934

Quoted 6 lines
contrib/libvhost-user/libvhost-user.c +4 -2
-    if (vmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK ||
-        vmsg->fd_num != 1) {
+    if (nofd) {
+        vmsg_close_fds(vmsg);
+        return true;
+    }

So in this particular code you quoted, I actually just aligned to have the same "bool nofd" variable - and I made it return "true" when no FD was given.

It couldn't make use of what you proposed:

Quoted 4 lines

With the following change to vmsg_close_fds():

  for (i = 0; i < vmsg->fd_num; i++) {
      close(vmsg->fds[i]);
Quoted 8 lines

}
+ for (i = 0; i < sizeof(vmsg->fd_num) / sizeof(vmsg->fd_num[0]); i++) {
+ vmsg->fds[i] = -1;
+ }
+ vmsg->fd_num = 0;

...the message handler functions below can use vmsg->fds[0] (-1) without worrying about NOFD. This makes the code simpler.

because fd_num != 1 leads to the original code returning false, which leads to the ring not getting started in vu_set_vring_kick_exec(). So we need the special code here, can be argued if I should pull out the test into the "bool nofd" variable or not ... *shrug*

The changes in vu_set_vring_kick_exec() and vu_set_vring_err_exec() would indeed then not be necessary, but in vu_set_vring_call_exec() we should still avoid the eventfd_write() if it's going to get -1.

So, yeah - could be a bit simpler there. I'd say being explicit here is easier to understand and thus nicer, but your (or Michael's I guess?) call.

johannes

Reviewed-by
#5 lore raw
Quoted 44 lines · Johannes Berg
Quoted 12 lines · Stefan Hajnoczi
Quoted 12 lines

vu_check_queue_msg_file(VuDev *dev, VhostUserMsg *vmsg)

 {
     int index = vmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
contrib/libvhost-user/libvhost-user.c +1 -0
+    bool nofd = vmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK;
 
     if (index >= dev->max_queues) {
         vmsg_close_fds(vmsg);
@@ -927,8 +928,12 @@ vu_check_queue_msg_file(VuDev *dev, VhostUserMsg *vmsg)
         return false;
     }
 
Quoted 6 lines

contrib/libvhost-user/libvhost-user.c vu_check_queue_msg_file(VuDev *dev, VhostUserMsg *vmsg) · line 934

Quoted 6 lines
contrib/libvhost-user/libvhost-user.c +4 -2
-    if (vmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK ||
-        vmsg->fd_num != 1) {
+    if (nofd) {
+        vmsg_close_fds(vmsg);
+        return true;
+    }

So in this particular code you quoted, I actually just aligned to have the same "bool nofd" variable - and I made it return "true" when no FD was given.

It couldn't make use of what you proposed:

Quoted 4 lines

With the following change to vmsg_close_fds():

  for (i = 0; i < vmsg->fd_num; i++) {
      close(vmsg->fds[i]);
Quoted 8 lines

}
+ for (i = 0; i < sizeof(vmsg->fd_num) / sizeof(vmsg->fd_num[0]); i++) {
+ vmsg->fds[i] = -1;
+ }
+ vmsg->fd_num = 0;

...the message handler functions below can use vmsg->fds[0] (-1) without worrying about NOFD. This makes the code simpler.

because fd_num != 1 leads to the original code returning false, which leads to the ring not getting started in vu_set_vring_kick_exec(). So we need the special code here, can be argued if I should pull out the test into the "bool nofd" variable or not ... *shrug*

Quoted 8 lines

The changes in vu_set_vring_kick_exec() and vu_set_vring_err_exec() would indeed then not be necessary, but in vu_set_vring_call_exec() we should still avoid the eventfd_write() if it's going to get -1.

So, yeah - could be a bit simpler there. I'd say being explicit here is easier to understand and thus nicer, but your (or Michael's I guess?) call.

Yeah, there is a trade-off to hiding NOFD and if what I proposed isn't convincing then it wasn't a good proposal :-):