Skip to contentSkip to conversation

Compiler warning fixes for libvhost-user,libvduse

PATCH Superseded Marcel Holtmann 3 participants 17 messages Dec 20, 2022 – Dec 21, 2022

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover Compiler warning fixes for libvhost-user,libvduse

The libvhost-user and libvduse libraries are also useful for external usage outside of QEMU and thus it would be nice if their files could be just copied and used. However due to different compiler settings, a lot of manual fixups are needed. This is the first attempt at some obvious fixes that can be done without any harm to the code and its readability.

Please note that 10/10 in this series is marked as RFC since I have no idea what is correct here. From the structures point of view the assignment that is done makes no sense to me. I might have to dig into specification to figure it out or it better be commented accordingly to tell the compiler that it got this part wrong.

Marcel Holtmann (10):
libvhost-user: Provide _GNU_SOURCE when compiling outside of QEMU
libvhost-user: Replace typeof with __typeof__
libvhost-user: Cast rc variable to avoid compiler warning
libvhost-user: Use unsigned int i for some for-loop iterations
libvhost-user: Declare uffdio_register early to make it C90 compliant
libvhost-user: Change dev->postcopy_ufd assignment to make it C90 compliant
libvhost-user: Switch to unsigned int for inuse field in struct VuVirtq
libvduse: Provide _GNU_SOURCE when compiling outside of QEMU
libvduse: Switch to unsigned int for inuse field in struct VduseVirtq
libvduse: Fix assignment in vring_set_avail_event

 subprojects/libvduse/libvduse.c           | 11 ++++++--
 subprojects/libvhost-user/libvhost-user.c | 31 ++++++++++++++---------
 subprojects/libvhost-user/libvhost-user.h |  2 +-
 3 files changed, 29 insertions(+), 15 deletions(-)
Signature · 1 line
2.38.1
#1 lore raw

PATCH v2 1/10 libvhost-user: Provide _GNU_SOURCE when compiling outside of QEMU

Then the libvhost-user sources are used by another project, it can not be guaranteed that _GNU_SOURCE is set by the build system. If it is for example not set, errors like this show up.

CC libvhost-user.o
libvhost-user.c: In function ‘vu_panic’:
libvhost-user.c:195:9: error: implicit declaration of function ‘vasprintf’; did you mean ‘vsprintf’? [-Werror=implicit-function-declaration]

  195 |     if (vasprintf(&buf, msg, ap) < 0) {
      |         ^~~~~~~~~
      |         vsprintf

The simplest way to allow external complication of libvhost-user.[ch] is by setting _GNU_SOURCE if it is not already set by the build system.

---
 subprojects/libvhost-user/libvhost-user.c | 4 ++++
 1 file changed, 4 insertions(+)
subprojects/libvhost-user/libvhost-user.c +4 -0
index d6ee6e7d9168..b55b9e244d9a 100644
@@ -13,6 +13,10 @@
  * later.  See the COPYING file in the top-level directory.
  */
 
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+
 /* this code avoids GLib dependency */
 #include <stdlib.h>
 #include <stdio.h>
Signature · 1 line
2.38.1
#2 lore raw

PATCH v2 2/10 libvhost-user: Replace typeof with __typeof__

In reply to Marcel Holtmann

Strictly speaking only -std=gnu99 support the usage of typeof and for easier inclusion in external projects, it is better to use __typeof__.

CC libvhost-user.o
libvhost-user.c: In function ‘vu_log_queue_fill’:
libvhost-user.c:86:13: error: implicit declaration of function ‘typeof’ [-Werror=implicit-function-declaration]

   86 |             typeof(x) _min1 = (x);              \
      |             ^~~~~~

Changing these two users of typeof makes the compiler happy and no extra flags or pragmas need to be provided.

---
 subprojects/libvhost-user/libvhost-user.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
subprojects/libvhost-user/libvhost-user.c +2 -2
index b55b9e244d9a..67d75ece53b7 100644
@@ -62,8 +62,8 @@
 #endif  /* !__GNUC__ */
 #ifndef MIN
 #define MIN(x, y) ({                            \
-            typeof(x) _min1 = (x);              \
-            typeof(y) _min2 = (y);              \
+            __typeof__(x) _min1 = (x);          \
+            __typeof__(y) _min2 = (y);          \
             (void) (&_min1 == &_min2);          \
             _min1 < _min2 ? _min1 : _min2; })
 #endif
Signature · 1 line
2.38.1
Reviewed-by on v2 2/10
#3 lore raw

Re: [PATCH v2 02/10] libvhost-user: Replace typeof with __typeof__

Quoted 9 lines · Marcel Holtmann

Strictly speaking only -std=gnu99 support the usage of typeof and for easier inclusion in external projects, it is better to use __typeof__.

CC libvhost-user.o
libvhost-user.c: In function ‘vu_log_queue_fill’:
libvhost-user.c:86:13: error: implicit declaration of function ‘typeof’ [-Werror=implicit-function-declaration]

    86 |             typeof(x) _min1 = (x);              \
       |             ^~~~~~
Quoted 7 lines

Changing these two users of typeof makes the compiler happy and no extra flags or pragmas need to be provided.

---
  subprojects/libvhost-user/libvhost-user.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
#4 lore raw

PATCH v2 3/10 libvhost-user: Cast rc variable to avoid compiler warning

In reply to Marcel Holtmann

The assert from recvmsg() return value against an uint32_t size field from a protocol struct throws a compiler warning.

CC libvhost-user.o
In file included from libvhost-user.c:27:
libvhost-user.c: In function ‘vu_message_read_default’:
libvhost-user.c:363:19: error: comparison of integer expressions of different signedness: ‘int’ and ‘uint32_t’ {aka ‘unsigned int’} [-Werror=sign-compare]
363 | assert(rc == vmsg->size);
| ^~

This is not critical, but annoying when the libvhost-user source are used in an external project that has this compiler warning switched on.

---
 subprojects/libvhost-user/libvhost-user.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
subprojects/libvhost-user/libvhost-user.c +1 -1
index 67d75ece53b7..bcdf32a24f60 100644
@@ -339,7 +339,7 @@ vu_message_read_default(VuDev *dev, int conn_fd, VhostUserMsg *vmsg)
             goto fail;
         }
 
-        assert(rc == vmsg->size);
+        assert((uint32_t)rc == vmsg->size);
     }
 
     return true;
Signature · 1 line
2.38.1
#5 lore raw

PATCH v2 4/10 libvhost-user: Use unsigned int i for some for-loop iterations

In reply to Marcel Holtmann

The sign-compare warning also hits some of the for-loops, but it easy fixed by just making the iterator variable unsigned int.

CC libvhost-user.o
libvhost-user.c: In function ‘vu_gpa_to_va’:
libvhost-user.c:223:19: error: comparison of integer expressions of different signedness: ‘int’ and ‘uint32_t’ {aka ‘unsigned int’} [-Werror=sign-compare]
223 | for (i = 0; i < dev->nregions; i++) {
| ^

---
 subprojects/libvhost-user/libvhost-user.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
subprojects/libvhost-user/libvhost-user.c +7 -7
index bcdf32a24f60..211d31a4cc88 100644
@@ -192,7 +192,7 @@ vu_panic(VuDev *dev, const char *msg, ...)
 void *
 vu_gpa_to_va(VuDev *dev, uint64_t *plen, uint64_t guest_addr)
 {
-    int i;
+    unsigned int i;
 
     if (*plen == 0) {
         return NULL;
@@ -218,7 +218,7 @@ vu_gpa_to_va(VuDev *dev, uint64_t *plen, uint64_t guest_addr)
 static void *
 qva_to_va(VuDev *dev, uint64_t qemu_addr)
 {
-    int i;
+    unsigned int i;
 
     /* Find matching memory region.  */
     for (i = 0; i < dev->nregions; i++) {
@@ -621,7 +621,7 @@ map_ring(VuDev *dev, VuVirtq *vq)
 
 static bool
 generate_faults(VuDev *dev) {
-    int i;
+    unsigned int i;
     for (i = 0; i < dev->nregions; i++) {
         VuDevRegion *dev_region = &dev->regions[i];
         int ret;
@@ -829,7 +829,7 @@ static inline bool reg_equal(VuDevRegion *vudev_reg,
 static bool
 vu_rem_mem_reg(VuDev *dev, VhostUserMsg *vmsg) {
     VhostUserMemoryRegion m = vmsg->payload.memreg.region, *msg_region = &m;
-    int i;
+    unsigned int i;
     bool found = false;
 
     if (vmsg->fd_num > 1) {
@@ -895,7 +895,7 @@ vu_rem_mem_reg(VuDev *dev, VhostUserMsg *vmsg) {
 static bool
 vu_set_mem_table_exec_postcopy(VuDev *dev, VhostUserMsg *vmsg)
 {
-    int i;
+    unsigned int i;
     VhostUserMemory m = vmsg->payload.memory, *memory = &m;
     dev->nregions = memory->nregions;
 
@@ -972,7 +972,7 @@ vu_set_mem_table_exec_postcopy(VuDev *dev, VhostUserMsg *vmsg)
 static bool
 vu_set_mem_table_exec(VuDev *dev, VhostUserMsg *vmsg)
 {
-    int i;
+    unsigned int i;
     VhostUserMemory m = vmsg->payload.memory, *memory = &m;
 
     for (i = 0; i < dev->nregions; i++) {
@@ -1980,7 +1980,7 @@ end:
 void
 vu_deinit(VuDev *dev)
 {
-    int i;
+    unsigned int i;
 
     for (i = 0; i < dev->nregions; i++) {
         VuDevRegion *r = &dev->regions[i];
Signature · 1 line
2.38.1
#6 lore raw

PATCH v2 5/10 libvhost-user: Declare uffdio_register early to make it C90 compliant

In reply to Marcel Holtmann

When using libvhost-user source in an external project that wants to comply with the C90 standard, it is best to declare variables before code.

CC libvhost-user.o
libvhost-user.c: In function ‘generate_faults’:
libvhost-user.c:683:9: error: ISO C90 forbids mixed declarations and code [-Werror=declaration-after-statement]

  683 |         struct uffdio_register reg_struct;
      |         ^~~~~~

In this case, it is also simple enough and doesn't cause any extra ifdef additions.

---
 subprojects/libvhost-user/libvhost-user.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
subprojects/libvhost-user/libvhost-user.c +3 -1
index 211d31a4cc88..bf92cc85c086 100644
@@ -626,6 +626,8 @@ generate_faults(VuDev *dev) {
         VuDevRegion *dev_region = &dev->regions[i];
         int ret;
 #ifdef UFFDIO_REGISTER
+        struct uffdio_register reg_struct;
+
         /*
          * We should already have an open ufd. Mark each memory
          * range as ufd.
@@ -659,7 +661,7 @@ generate_faults(VuDev *dev) {
                     "%s: Failed to madvise(NOHUGEPAGE) region %d: %s\n",
                     __func__, i, strerror(errno));
         }
-        struct uffdio_register reg_struct;
+
         reg_struct.range.start = (uintptr_t)dev_region->mmap_addr;
         reg_struct.range.len = dev_region->size + dev_region->mmap_offset;
         reg_struct.mode = UFFDIO_REGISTER_MODE_MISSING;
Signature · 1 line
2.38.1
Reviewed-by on v2 5/10
#7 lore raw

Re: [PATCH v2 05/10] libvhost-user: Declare uffdio_register early to make it C90 compliant

Quoted 10 lines · Marcel Holtmann

When using libvhost-user source in an external project that wants to comply with the C90 standard, it is best to declare variables before code.

CC libvhost-user.o
libvhost-user.c: In function ‘generate_faults’:
libvhost-user.c:683:9: error: ISO C90 forbids mixed declarations and code [-Werror=declaration-after-statement]

   683 |         struct uffdio_register reg_struct;
       |         ^~~~~~
Quoted 7 lines

In this case, it is also simple enough and doesn't cause any extra ifdef additions.

---
  subprojects/libvhost-user/libvhost-user.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
#8 lore raw

PATCH v2 6/10 libvhost-user: Change dev->postcopy_ufd assignment to make it C90 compliant

In reply to Marcel Holtmann

The assignment of dev->postcopy_ufd can be moved into an else clause and then the code becomes C90 compliant.

CC libvhost-user.o
libvhost-user.c: In function ‘vu_set_postcopy_advise’:
libvhost-user.c:1625:5: error: ISO C90 forbids mixed declarations and code [-Werror=declaration-after-statement]

 1625 |     struct uffdio_api api_struct;
      |     ^~~~~~

Understandable, it might be desired to avoid else clauses, but in this case it seems clear enough and frankly the dev->postcopy_ufd is only assigned once.

---
 subprojects/libvhost-user/libvhost-user.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
subprojects/libvhost-user/libvhost-user.c +2 -1
index bf92cc85c086..b28b66cdb159 100644
@@ -1599,12 +1599,13 @@ vu_set_config(VuDev *dev, VhostUserMsg *vmsg)
 static bool
 vu_set_postcopy_advise(VuDev *dev, VhostUserMsg *vmsg)
 {
-    dev->postcopy_ufd = -1;
 #ifdef UFFDIO_API
     struct uffdio_api api_struct;
 
     dev->postcopy_ufd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
     vmsg->size = 0;
+#else
+    dev->postcopy_ufd = -1;
 #endif
 
     if (dev->postcopy_ufd == -1) {
Signature · 1 line
2.38.1
Reviewed-by on v2 6/10
#9 lore raw

Re: [PATCH v2 06/10] libvhost-user: Change dev->postcopy_ufd assignment to make it C90 compliant

Quoted 9 lines · Marcel Holtmann

The assignment of dev->postcopy_ufd can be moved into an else clause and then the code becomes C90 compliant.

CC libvhost-user.o
libvhost-user.c: In function ‘vu_set_postcopy_advise’:
libvhost-user.c:1625:5: error: ISO C90 forbids mixed declarations and code [-Werror=declaration-after-statement]

  1625 |     struct uffdio_api api_struct;
       |     ^~~~~~
Quoted 8 lines

Understandable, it might be desired to avoid else clauses, but in this case it seems clear enough and frankly the dev->postcopy_ufd is only assigned once.

---
  subprojects/libvhost-user/libvhost-user.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
#10 lore raw

PATCH v2 7/10 libvhost-user: Switch to unsigned int for inuse field in struct VuVirtq

In reply to Marcel Holtmann

It seems there is no need to keep the inuse field signed and end up with compiler warnings for sign-compare.

CC libvhost-user.o
libvhost-user.c: In function ‘vu_queue_pop’:
libvhost-user.c:2763:19: error: comparison of integer expressions of different signedness: ‘int’ and ‘unsigned int’ [-Werror=sign-compare]
2763 | if (vq->inuse >= vq->vring.num) {
| ^~
libvhost-user.c: In function ‘vu_queue_rewind’:
libvhost-user.c:2808:13: error: comparison of integer expressions of different signedness: ‘unsigned int’ and ‘int’ [-Werror=sign-compare]
2808 | if (num > vq->inuse) {
| ^

Instead of casting the comparison to unsigned int, just make the inuse field unsigned int in the fist place.

---
 subprojects/libvhost-user/libvhost-user.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
subprojects/libvhost-user/libvhost-user.h +1 -1
index aea7ec5061d5..8cda9b8f577a 100644
@@ -343,7 +343,7 @@ typedef struct VuVirtq {
     /* Notification enabled? */
     bool notification;
 
-    int inuse;
+    unsigned int inuse;
 
     vu_queue_handler_cb handler;
 
Signature · 1 line
2.38.1
#11 lore raw

PATCH v2 8/10 libvduse: Provide _GNU_SOURCE when compiling outside of QEMU

In reply to Marcel Holtmann

When the libvduse sources are used by another project, it can not be guaranteed that _GNU_SOURCE is set by the build system. If it is for example not set, errors like this show up.

CC libvduse.o
libvduse.c: In function ‘vduse_log_get’:
libvduse.c:172:9: error: implicit declaration of function ‘ftruncate’; did you mean ‘strncat’? [-Werror=implicit-function-declaration]

  172 |     if (ftruncate(fd, size) == -1) {
      |         ^~~~~~~~~
      |         strncat

The simplest way to allow external complication of libvduse.[ch] by setting _GNU_SOURCE if it is not already set by the build system.

---
 subprojects/libvduse/libvduse.c | 4 ++++
 1 file changed, 4 insertions(+)
subprojects/libvduse/libvduse.c +4 -0
index e089d4d546cf..c871bd331a6b 100644
@@ -16,6 +16,10 @@
  * later.  See the COPYING file in the top-level directory.
  */
 
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+
 #include <stdlib.h>
 #include <stdio.h>
 #include <stdbool.h>
Signature · 1 line
2.38.1
#12 lore raw

PATCH v2 9/10 libvduse: Switch to unsigned int for inuse field in struct VduseVirtq

In reply to Marcel Holtmann

It seems there is no need to keep the inuse field signed and end up with compiler warnings for sign-compare.

CC libvduse.o
libvduse.c: In function ‘vduse_queue_pop’:
libvduse.c:789:19: error: comparison of integer expressions of different signedness: ‘int’ and ‘unsigned int’ [-Werror=sign-compare]
789 | if (vq->inuse >= vq->vring.num) {
| ^~

Instead of casting the comparison to unsigned int, just make the inuse field unsigned int in the fist place.

---
 subprojects/libvduse/libvduse.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
subprojects/libvduse/libvduse.c +1 -1
index c871bd331a6b..338ad5e352e7 100644
@@ -101,7 +101,7 @@ struct VduseVirtq {
     uint16_t signalled_used;
     bool signalled_used_valid;
     int index;
-    int inuse;
+    unsigned int inuse;
     bool ready;
     int fd;
     VduseDev *dev;
Signature · 1 line
2.38.1
Reviewed-by on v2 9/10
#13 lore raw

Re: [PATCH v2 09/10] libvduse: Switch to unsigned int for inuse field in struct VduseVirtq

Quoted 10 lines · Marcel Holtmann

It seems there is no need to keep the inuse field signed and end up with compiler warnings for sign-compare.

CC libvduse.o
libvduse.c: In function ‘vduse_queue_pop’:
libvduse.c:789:19: error: comparison of integer expressions of different signedness: ‘int’ and ‘unsigned int’ [-Werror=sign-compare]
789 | if (vq->inuse >= vq->vring.num) {
| ^~

Quoted 8 lines

Instead of casting the comparison to unsigned int, just make the inuse field unsigned int in the fist place.

---
 subprojects/libvduse/libvduse.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Thanks,
Yongji

#14 lore raw

RFC v2 10/10 libvduse: Fix assignment in vring_set_avail_event

In reply to Marcel Holtmann

CC libvduse.o
libvduse.c: In function ‘vring_set_avail_event’:
libvduse.c:603:7: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasin]

  603 |     *((uint16_t *)&vq->vring.used->ring[vq->vring.num]) = htole16(val);
      |      ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
 subprojects/libvduse/libvduse.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
subprojects/libvduse/libvduse.c +4 -1
index 338ad5e352e7..51a4ba1b6878 100644
@@ -582,7 +582,10 @@ void vduse_queue_notify(VduseVirtq *vq)
 
 static inline void vring_set_avail_event(VduseVirtq *vq, uint16_t val)
 {
-    *((uint16_t *)&vq->vring.used->ring[vq->vring.num]) = htole16(val);
+    vring_used_elem_t *ring = &vq->vring.used->ring[vq->vring.num];
+
+    /* FIXME: Is this actually correct since this is __virtio32 id; */
+    ring->id = htole16(val);
 }
 
 static bool vduse_queue_map_single_desc(VduseVirtq *vq, unsigned int *p_num_sg,
Signature · 1 line
2.38.1
#15 lore raw

Re: [RFC v2 10/10] libvduse: Fix assignment in vring_set_avail_event

Quoted 17 lines · Marcel Holtmann

CC libvduse.o
libvduse.c: In function ‘vring_set_avail_event’:
libvduse.c:603:7: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasin]

  603 |     *((uint16_t *)&vq->vring.used->ring[vq->vring.num]) = htole16(val);
      |      ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
 subprojects/libvduse/libvduse.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
subprojects/libvduse/libvduse.c +0 -0
index 338ad5e352e7..51a4ba1b6878 100644

subprojects/libvduse/libvduse.c void vduse_queue_notify(VduseVirtq *vq) · line 589

Quoted 9 lines
subprojects/libvduse/libvduse.c +4 -1
@@ -582,7 +582,10 @@ void vduse_queue_notify(VduseVirtq *vq)
 
 static inline void vring_set_avail_event(VduseVirtq *vq, uint16_t val)
 {
-    *((uint16_t *)&vq->vring.used->ring[vq->vring.num]) = htole16(val);
+    vring_used_elem_t *ring = &vq->vring.used->ring[vq->vring.num];
+
+    /* FIXME: Is this actually correct since this is __virtio32 id; */
+    ring->id = htole16(val);
 }

Can we do it as libvhost-user does?

static inline void
vring_set_avail_event(VuVirtq *vq, uint16_t val)

{
    uint16_t *avail;

    avail = (uint16_t *)&vq->vring.used->ring[vq->vring.num];
    *avail = htole16(val);
}

Thanks,
Yongji

#16 lore raw

Re: [RFC v2 10/10] libvduse: Fix assignment in vring_set_avail_event

Hi Yongji,

Quoted 28 lines
Quoted 16 lines

CC libvduse.o
libvduse.c: In function ‘vring_set_avail_event’:
libvduse.c:603:7: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasin]

 603 |     *((uint16_t *)&vq->vring.used->ring[vq->vring.num]) = htole16(val);
     |      ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---
subprojects/libvduse/libvduse.c | 5 ++++-

1 file changed, 4 insertions(+), 1 deletion(-)
subprojects/libvduse/libvduse.c +0 -0
index 338ad5e352e7..51a4ba1b6878 100644

subprojects/libvduse/libvduse.c void vduse_queue_notify(VduseVirtq *vq) · line 589

Quoted 9 lines
subprojects/libvduse/libvduse.c +4 -1
@@ -582,7 +582,10 @@ void vduse_queue_notify(VduseVirtq *vq)
 
 static inline void vring_set_avail_event(VduseVirtq *vq, uint16_t val)
 {
-    *((uint16_t *)&vq->vring.used->ring[vq->vring.num]) = htole16(val);
+    vring_used_elem_t *ring = &vq->vring.used->ring[vq->vring.num];
+
+    /* FIXME: Is this actually correct since this is __virtio32 id; */
+    ring->id = htole16(val);
 }

Can we do it as libvhost-user does?

Quoted 8 lines

static inline void
vring_set_avail_event(VuVirtq *vq, uint16_t val)

{
   uint16_t *avail;

   avail = (uint16_t *)&vq->vring.used->ring[vq->vring.num];
   *avail = htole16(val);
}

that will also work. Sending a v3 in a few moments.

Regards

Marcel