Skip to contentSkip to conversation

virtio-gpu: use consistent error checking style for virtio_gpu_create_mapping_iov

PATCH Superseded Honglei Huang 4 participants 13 messages Nov 17, 2025 – Nov 20, 2025

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 virtio-gpu: use consistent error checking style for virtio_gpu_create_mapping_iov

Fix error handling logic in virgl_cmd_resource_create_blob and improve consistency across the codebase.

virtio_gpu_create_mapping_iov() returns 0 on success and negative values on error, but the original code was inconsistently checking for error conditions using different patterns.

Change all virtio_gpu_create_mapping_iov() error checks to use consistent 'ret < 0' or 'ret >= 0' patterns, following the preferred QEMU coding convention for functions that return 0 on success and negative on error. This makes the return value convention immediately clear to code readers without needing to look up the function definition.

Updated locations:
- hw/display/virtio-gpu-virgl.c: virgl_cmd_resource_create_blob()
- hw/display/virtio-gpu-virgl.c: virgl_cmd_resource_attach_backing()
- hw/display/virtio-gpu.c: virtio_gpu_resource_create_blob()
- hw/display/virtio-gpu.c: virtio_gpu_resource_attach_backing()
- hw/display/virtio-gpu-rutabaga.c: rutabaga_cmd_attach_backing()
- hw/display/virtio-gpu-rutabaga.c: rutabaga_cmd_resource_create_blob()

Changes since v3:
- Extended consistency improvements to virtio-gpu-rutabaga.c
- Changed CHECK(!ret) to CHECK(ret >= 0) and CHECK(!result) to CHECK(result >= 0) in rutabaga functions for consistency
- Now covers all virtio-gpu files that use virtio_gpu_create_mapping_iov()

Changes since v2:
- Use 'if (ret < 0)' instead of 'if (ret != 0)' following maintainer's feedback on preferred QEMU coding style for error checking functions that return 0 on success and negative on error
- Updated all similar usages across virtio-gpu files for consistency
- Expanded scope from single function fix to codebase-wide style consistency

Fixes 7c092f17ccee ("virtio-gpu: Handle resource blob commands")
---
 hw/display/virtio-gpu-rutabaga.c | 4 ++--
 hw/display/virtio-gpu-virgl.c    | 4 ++--
 hw/display/virtio-gpu.c          | 4 ++--
 3 files changed, 6 insertions(+), 6 deletions(-)
hw/display/virtio-gpu-rutabaga.c +2 -2
index ed5ae52acb..ea2928b706 100644
@@ -466,7 +466,7 @@ rutabaga_cmd_attach_backing(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd)
 
     ret = virtio_gpu_create_mapping_iov(g, att_rb.nr_entries, sizeof(att_rb),
                                         cmd, NULL, &res->iov, &res->iov_cnt);
-    CHECK(!ret, cmd);
+    CHECK(ret >= 0, cmd);
 
     vecs.iovecs = res->iov;
     vecs.num_iovecs = res->iov_cnt;
@@ -616,7 +616,7 @@ rutabaga_cmd_resource_create_blob(VirtIOGPU *g,
         result = virtio_gpu_create_mapping_iov(g, cblob.nr_entries,
                                                sizeof(cblob), cmd, &res->addrs,
                                                &res->iov, &res->iov_cnt);
-        CHECK(!result, cmd);
+        CHECK(result >= 0, cmd);
     }
 
     rc_blob.blob_id = cblob.blob_id;
hw/display/virtio-gpu-virgl.c +2 -2
index 94ddc01f91..6ebd9293e5 100644
@@ -557,7 +557,7 @@ static void virgl_resource_attach_backing(VirtIOGPU *g,
 
     ret = virtio_gpu_create_mapping_iov(g, att_rb.nr_entries, sizeof(att_rb),
                                         cmd, NULL, &res_iovs, &res_niov);
-    if (ret != 0) {
+    if (ret < 0) {
         cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
         return;
     }
@@ -701,7 +701,7 @@ static void virgl_cmd_resource_create_blob(VirtIOGPU *g,
         ret = virtio_gpu_create_mapping_iov(g, cblob.nr_entries, sizeof(cblob),
                                             cmd, &res->base.addrs,
                                             &res->base.iov, &res->base.iov_cnt);
-        if (!ret) {
+        if (ret < 0) {
             cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
             return;
         }
hw/display/virtio-gpu.c +2 -2
index 0a1a625b0e..1038c6a49f 100644
@@ -352,7 +352,7 @@ static void virtio_gpu_resource_create_blob(VirtIOGPU *g,
     ret = virtio_gpu_create_mapping_iov(g, cblob.nr_entries, sizeof(cblob),
                                         cmd, &res->addrs, &res->iov,
                                         &res->iov_cnt);
-    if (ret != 0) {
+    if (ret < 0) {
         cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
         g_free(res);
         return;
@@ -931,7 +931,7 @@ virtio_gpu_resource_attach_backing(VirtIOGPU *g,
 
     ret = virtio_gpu_create_mapping_iov(g, ab.nr_entries, sizeof(ab), cmd,
                                         &res->addrs, &res->iov, &res->iov_cnt);
-    if (ret != 0) {
+    if (ret < 0) {
         cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
         return;
     }
Signature · 1 line
2.34.1
Reviewed-by on v4 1/1
#1 lore raw
Quoted 34 lines · Honglei Huang

Fix error handling logic in virgl_cmd_resource_create_blob and improve consistency across the codebase.

virtio_gpu_create_mapping_iov() returns 0 on success and negative values on error, but the original code was inconsistently checking for error conditions using different patterns.

Change all virtio_gpu_create_mapping_iov() error checks to use consistent 'ret < 0' or 'ret >= 0' patterns, following the preferred QEMU coding convention for functions that return 0 on success and negative on error. This makes the return value convention immediately clear to code readers without needing to look up the function definition.

Updated locations:
- hw/display/virtio-gpu-virgl.c: virgl_cmd_resource_create_blob()
- hw/display/virtio-gpu-virgl.c: virgl_cmd_resource_attach_backing()
- hw/display/virtio-gpu.c: virtio_gpu_resource_create_blob()
- hw/display/virtio-gpu.c: virtio_gpu_resource_attach_backing()
- hw/display/virtio-gpu-rutabaga.c: rutabaga_cmd_attach_backing()
- hw/display/virtio-gpu-rutabaga.c: rutabaga_cmd_resource_create_blob()

Changes since v3:
- Extended consistency improvements to virtio-gpu-rutabaga.c
- Changed CHECK(!ret) to CHECK(ret >= 0) and CHECK(!result) to CHECK(result >= 0) in rutabaga functions for consistency
- Now covers all virtio-gpu files that use virtio_gpu_create_mapping_iov()

Changes since v2:
- Use 'if (ret < 0)' instead of 'if (ret != 0)' following maintainer's feedback on preferred QEMU coding style for error checking functions that return 0 on success and negative on error
- Updated all similar usages across virtio-gpu files for consistency
- Expanded scope from single function fix to codebase-wide style consistency

Quoted 3 lines
Fixes 7c092f17ccee ("virtio-gpu: Handle resource blob commands")
#2 lore raw
In reply to Honglei Huang

hw/display/virtio-gpu-rutabaga.c rutabaga_cmd_attach_backing(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd) · line 469

Quoted 10 lines · Honglei Huang
hw/display/virtio-gpu-rutabaga.c +1 -1
index ed5ae52acb..ea2928b706 100644
@@ -466,7 +466,7 @@ rutabaga_cmd_attach_backing(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd)
 
     ret = virtio_gpu_create_mapping_iov(g, att_rb.nr_entries, sizeof(att_rb),
                                         cmd, NULL, &res->iov, &res->iov_cnt);
-    CHECK(!ret, cmd);
+    CHECK(ret >= 0, cmd);

virtio_gpu_create_mapping_iov() doesn't return positive values, don't see how this change improves anything. You now saying that ret > 0 is okay, while it shall never happen.

Signature · 2 lines
Best regards,
Dmitry
#3 lore raw
Quoted 7 lines · Dmitry Osipenko
Quoted 6 lines · Honglei Huang
hw/display/virtio-gpu-rutabaga.c +0 -0
index ed5ae52acb..ea2928b706 100644
@@ -466,7 +466,7 @@ rutabaga_cmd_attach_backing(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd)
 
Quoted 8 lines

hw/display/virtio-gpu-rutabaga.c rutabaga_cmd_attach_backing(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd) · line 469

Quoted 4 lines
hw/display/virtio-gpu-rutabaga.c +1 -1
     ret = virtio_gpu_create_mapping_iov(g, att_rb.nr_entries, sizeof(att_rb),
                                         cmd, NULL, &res->iov, &res->iov_cnt);
-    CHECK(!ret, cmd);
+    CHECK(ret >= 0, cmd);

virtio_gpu_create_mapping_iov() doesn't return positive values, don't see how this change improves anything. You now saying that ret > 0 is okay, while it shall never happen.

Please see

    Subject: Re: [PATCH] virtio-gpu-virgl: fix error handling in virgl_cmd_resource_create_blob 
    Date: Mon, 17 Nov 2025 08:49:42 +0100
    Message-ID: <[email protected]>
    https://lore.kernel.org/qemu-devel/[email protected]/
#4 lore raw
Quoted 18 lines · Markus Armbruster
Quoted 7 lines · Dmitry Osipenko
Quoted 6 lines · Honglei Huang
hw/display/virtio-gpu-rutabaga.c +0 -0
index ed5ae52acb..ea2928b706 100644
@@ -466,7 +466,7 @@ rutabaga_cmd_attach_backing(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd)
 
Quoted 8 lines

hw/display/virtio-gpu-rutabaga.c rutabaga_cmd_attach_backing(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd) · line 469

Quoted 4 lines
hw/display/virtio-gpu-rutabaga.c +1 -1
     ret = virtio_gpu_create_mapping_iov(g, att_rb.nr_entries, sizeof(att_rb),
                                         cmd, NULL, &res->iov, &res->iov_cnt);
-    CHECK(!ret, cmd);
+    CHECK(ret >= 0, cmd);

virtio_gpu_create_mapping_iov() doesn't return positive values, don't see how this change improves anything. You now saying that ret > 0 is okay, while it shall never happen.

Quoted 6 lines

Please see

    Subject: Re: [PATCH] virtio-gpu-virgl: fix error handling in virgl_cmd_resource_create_blob 
    Date: Mon, 17 Nov 2025 08:49:42 +0100
    Message-ID: <[email protected]>
    https://lore.kernel.org/qemu-devel/[email protected]/

It's a rather common bug when errno isn't negated by mistake and a positive error code is returned. Ignoring positive values when they aren't expected opens door to unnecessary problems, IMO.

Signature · 2 lines
Best regards,
Dmitry
#5 lore raw
Quoted 21 lines · Dmitry Osipenko
Quoted 20 lines · Markus Armbruster
Quoted 7 lines · Dmitry Osipenko
Quoted 6 lines · Honglei Huang
hw/display/virtio-gpu-rutabaga.c +0 -0
index ed5ae52acb..ea2928b706 100644
@@ -466,7 +466,7 @@ rutabaga_cmd_attach_backing(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd)
 
Quoted 8 lines

hw/display/virtio-gpu-rutabaga.c rutabaga_cmd_attach_backing(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd) · line 469

Quoted 4 lines
hw/display/virtio-gpu-rutabaga.c +1 -1
     ret = virtio_gpu_create_mapping_iov(g, att_rb.nr_entries, sizeof(att_rb),
                                         cmd, NULL, &res->iov, &res->iov_cnt);
-    CHECK(!ret, cmd);
+    CHECK(ret >= 0, cmd);

virtio_gpu_create_mapping_iov() doesn't return positive values, don't see how this change improves anything. You now saying that ret > 0 is okay, while it shall never happen.

Please see

Quoted 8 lines
Quoted 4 lines
    Subject: Re: [PATCH] virtio-gpu-virgl: fix error handling in virgl_cmd_resource_create_blob 
    Date: Mon, 17 Nov 2025 08:49:42 +0100
    Message-ID: <[email protected]>
    https://lore.kernel.org/qemu-devel/[email protected]/

It's a rather common bug when errno isn't negated by mistake and a positive error code is returned. Ignoring positive values when they aren't expected opens door to unnecessary problems, IMO.

No convention can avoid *all* possible problems there. I know which one has created more grief for *me*. Your experience may be different.

virtio_gpu_create_mapping_iov() returns -1 on error, 0 on success. We could change it to return false on error, true on success.

#6 lore raw
Quoted 32 lines · Markus Armbruster
Quoted 21 lines · Dmitry Osipenko
Quoted 20 lines · Markus Armbruster
Quoted 7 lines · Dmitry Osipenko
Quoted 6 lines · Honglei Huang
hw/display/virtio-gpu-rutabaga.c +0 -0
index ed5ae52acb..ea2928b706 100644
@@ -466,7 +466,7 @@ rutabaga_cmd_attach_backing(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd)
 
Quoted 8 lines

hw/display/virtio-gpu-rutabaga.c rutabaga_cmd_attach_backing(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd) · line 469

Quoted 4 lines
hw/display/virtio-gpu-rutabaga.c +1 -1
     ret = virtio_gpu_create_mapping_iov(g, att_rb.nr_entries, sizeof(att_rb),
                                         cmd, NULL, &res->iov, &res->iov_cnt);
-    CHECK(!ret, cmd);
+    CHECK(ret >= 0, cmd);

virtio_gpu_create_mapping_iov() doesn't return positive values, don't see how this change improves anything. You now saying that ret > 0 is okay, while it shall never happen.

Please see

Quoted 8 lines
Quoted 4 lines
    Subject: Re: [PATCH] virtio-gpu-virgl: fix error handling in virgl_cmd_resource_create_blob 
    Date: Mon, 17 Nov 2025 08:49:42 +0100
    Message-ID: <[email protected]>
    https://lore.kernel.org/qemu-devel/[email protected]/

It's a rather common bug when errno isn't negated by mistake and a positive error code is returned. Ignoring positive values when they aren't expected opens door to unnecessary problems, IMO.

Quoted 5 lines

No convention can avoid *all* possible problems there. I know which one has created more grief for *me*. Your experience may be different.

virtio_gpu_create_mapping_iov() returns -1 on error, 0 on success. We could change it to return false on error, true on success.

True/false are also often confusing. As was written in the other email, this variant is now good to me. In the end I'm glad for having this discussion, it will help with further improvement of virtio-gpu code.

Signature · 2 lines
Best regards,
Dmitry
#7 lore raw
In reply to Dmitry Osipenko
Quoted 21 lines · Dmitry Osipenko
Quoted 20 lines · Markus Armbruster
Quoted 7 lines · Dmitry Osipenko
Quoted 6 lines · Honglei Huang
hw/display/virtio-gpu-rutabaga.c +0 -0
index ed5ae52acb..ea2928b706 100644
@@ -466,7 +466,7 @@ rutabaga_cmd_attach_backing(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd)
 
Quoted 8 lines

hw/display/virtio-gpu-rutabaga.c rutabaga_cmd_attach_backing(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd) · line 469

Quoted 4 lines
hw/display/virtio-gpu-rutabaga.c +1 -1
      ret = virtio_gpu_create_mapping_iov(g, att_rb.nr_entries, sizeof(att_rb),
                                          cmd, NULL, &res->iov, &res->iov_cnt);
-    CHECK(!ret, cmd);
+    CHECK(ret >= 0, cmd);

virtio_gpu_create_mapping_iov() doesn't return positive values, don't see how this change improves anything. You now saying that ret > 0 is okay, while it shall never happen.

Please see

Quoted 9 lines
Quoted 4 lines
     Subject: Re: [PATCH] virtio-gpu-virgl: fix error handling in virgl_cmd_resource_create_blob
     Date: Mon, 17 Nov 2025 08:49:42 +0100
     Message-ID: <[email protected]>
     https://lore.kernel.org/qemu-devel/[email protected]/

It's a rather common bug when errno isn't negated by mistake and a positive error code is returned. Ignoring positive values when they aren't expected opens door to unnecessary problems, IMO.

How about apply the v2 or v3 firstly to fix the virtio_gpu_create_mapping_iov() block issue in virtio-gpu?

I will create another thread for the CHECK(!ret, cmd); thing in rutabaga.

Regards,
Honglei

#8 lore raw
Quoted 38 lines · Honglei Huang
Quoted 30 lines · Dmitry Osipenko
Quoted 21 lines · Markus Armbruster
Quoted 10 lines · Dmitry Osipenko
Quoted 9 lines · Honglei Huang
hw/display/virtio- renamed from hw/display/virtio-gpu-rutabaga.c +0 -0

gpu-rutabaga.c
index ed5ae52acb..ea2928b706 100644

hw/display/virtio-gpu-rutabaga.c +0 -0
@@ -466,7 +466,7 @@ rutabaga_cmd_attach_backing(VirtIOGPU *g,
 struct virtio_gpu_ctrl_command *cmd)
         ret = virtio_gpu_create_mapping_iov(g, att_rb.nr_entries,
 sizeof(att_rb),
Quoted 8 lines

hw/display/virtio-gpu-rutabaga.c rutabaga_cmd_attach_backing(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd) · line 469

Quoted 4 lines
hw/display/virtio-gpu-rutabaga.c +1 -1
                                           cmd, NULL, &res->iov,
 &res->iov_cnt);
-    CHECK(!ret, cmd);
+    CHECK(ret >= 0, cmd);

virtio_gpu_create_mapping_iov() doesn't return positive values, don't see how this change improves anything. You now saying that ret > 0 is okay, while it shall never happen.

Quoted 7 lines

Please see

     Subject: Re: [PATCH] virtio-gpu-virgl: fix error handling in virgl_cmd_resource_create_blob
     Date: Mon, 17 Nov 2025 08:49:42 +0100
     Message-ID: <[email protected]>
     https://lore.kernel.org/qemu-devel/[email protected]/

Quoted 4 lines

It's a rather common bug when errno isn't negated by mistake and a positive error code is returned. Ignoring positive values when they aren't expected opens door to unnecessary problems, IMO.

Quoted 4 lines

How about apply the v2 or v3 firstly to fix the virtio_gpu_create_mapping_iov() block issue in virtio-gpu?

I will create another thread for the CHECK(!ret, cmd); thing in rutabaga.

There was a precedent of virtio-gpu not handling positive error codes properly [1]. To me there is no problem that needs to be fixed when virtio_gpu_create_mapping_iov() is never expected to return positive values and doesn't return them.

[1]
https://lore.kernel.org/qemu-devel/[email protected]/

It's a common expectation that errors are negative. But in practice it's not always true, especially when interacting with external code.

Functionally this patch doesn't change anything. Will leave to Alex and Akihiko to decide on it.

Signature · 2 lines
Best regards,
Dmitry
#9 lore raw
Quoted 49 lines · Dmitry Osipenko
Quoted 38 lines · Honglei Huang
Quoted 30 lines · Dmitry Osipenko
Quoted 21 lines · Markus Armbruster
Quoted 10 lines · Dmitry Osipenko
Quoted 9 lines · Honglei Huang
hw/display/virtio- renamed from hw/display/virtio-gpu-rutabaga.c +0 -0

gpu-rutabaga.c
index ed5ae52acb..ea2928b706 100644

hw/display/virtio-gpu-rutabaga.c +0 -0
@@ -466,7 +466,7 @@ rutabaga_cmd_attach_backing(VirtIOGPU *g,
 struct virtio_gpu_ctrl_command *cmd)
         ret = virtio_gpu_create_mapping_iov(g, att_rb.nr_entries,
 sizeof(att_rb),
Quoted 8 lines

hw/display/virtio-gpu-rutabaga.c rutabaga_cmd_attach_backing(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd) · line 469

Quoted 4 lines
hw/display/virtio-gpu-rutabaga.c +1 -1
                                           cmd, NULL, &res->iov,
 &res->iov_cnt);
-    CHECK(!ret, cmd);
+    CHECK(ret >= 0, cmd);

virtio_gpu_create_mapping_iov() doesn't return positive values, don't see how this change improves anything. You now saying that ret > 0 is okay, while it shall never happen.

Quoted 7 lines

Please see

     Subject: Re: [PATCH] virtio-gpu-virgl: fix error handling in
virgl_cmd_resource_create_blob
     Date: Mon, 17 Nov 2025 08:49:42 +0100
     Message-ID: <[email protected]>
     https://lore.kernel.org/qemu-devel/[email protected]/

Quoted 4 lines

It's a rather common bug when errno isn't negated by mistake and a positive error code is returned. Ignoring positive values when they aren't expected opens door to unnecessary problems, IMO.

Quoted 4 lines

How about apply the v2 or v3 firstly to fix the virtio_gpu_create_mapping_iov() block issue in virtio-gpu?

I will create another thread for the CHECK(!ret, cmd); thing in rutabaga.

There was a precedent of virtio-gpu not handling positive error codes properly [1]. To me there is no problem that needs to be fixed when virtio_gpu_create_mapping_iov() is never expected to return positive values and doesn't return them.

Quoted 8 lines

[1]
https://lore.kernel.org/qemu-devel/[email protected]/

It's a common expectation that errors are negative. But in practice it's not always true, especially when interacting with external code.

Functionally this patch doesn't change anything. Will leave to Alex and Akihiko to decide on it.

I'm fine either way.

This is a "trade-off" scenario where both two options have upsides and downsides. I often avoid making an argument in that case because it tends to be subjective and cannot be confirmed, leading to bikeshedding.

In my opinion, such a function should not return int in the first place, but should return bool. The wide range of values int can take causes problems; bool allows compilers to enforce that it only returns two values.

Sign is one of those problems. int leads to a mistake like returning errno instead of -errno.

Functions may have completely different ideas for integer return values. In case of virtio-gpu, virtio_gpu_create_mapping_iov() returns -1 but virtio_gpu_update_dmabuf() returns -EINVAL. This is confusing.

Sometimes it is still preferred to keep functions returning int for consistency, but looking at include/hw/virtio/virtio-gpu.h, three return bool and three return int, so returning int here does not improve consistency.

That all said, this is a bug fix, not refactoring. This patch does fix a bug and avoids polluting the codebase, so it is fine for me. Perhaps it may be nice to have a patch to convert all functions returning int to bool to avoid pitfalls and improve consistency, but that can be done later.

Regards,
Akihiko Odaki

#10 lore raw
Quoted 83 lines · Akihiko Odaki
Quoted 54 lines · Dmitry Osipenko
Quoted 38 lines · Honglei Huang
Quoted 30 lines · Dmitry Osipenko
Quoted 21 lines · Markus Armbruster
Quoted 10 lines · Dmitry Osipenko
Quoted 9 lines · Honglei Huang
hw/display/virtio- renamed from hw/display/virtio-gpu-rutabaga.c +0 -0

gpu-rutabaga.c
index ed5ae52acb..ea2928b706 100644

hw/display/virtio-gpu-rutabaga.c +0 -0
@@ -466,7 +466,7 @@ rutabaga_cmd_attach_backing(VirtIOGPU *g,
 struct virtio_gpu_ctrl_command *cmd)
          ret = virtio_gpu_create_mapping_iov(g, att_rb.nr_entries,
 sizeof(att_rb),
Quoted 8 lines

hw/display/virtio-gpu-rutabaga.c rutabaga_cmd_attach_backing(VirtIOGPU *g, · line 471

Quoted 4 lines
hw/display/virtio-gpu-rutabaga.c +1 -1
                                            cmd, NULL, &res->iov,
 &res->iov_cnt);
-    CHECK(!ret, cmd);
+    CHECK(ret >= 0, cmd);

virtio_gpu_create_mapping_iov() doesn't return positive values, don't see how this change improves anything. You now saying that ret > 0 is okay, while it shall never happen.

Quoted 7 lines

Please see

      Subject: Re: [PATCH] virtio-gpu-virgl: fix error handling in virgl_cmd_resource_create_blob
      Date: Mon, 17 Nov 2025 08:49:42 +0100
      Message-ID: <[email protected]>
      https://lore.kernel.org/qemu-devel/[email protected]/

Quoted 4 lines

It's a rather common bug when errno isn't negated by mistake and a positive error code is returned. Ignoring positive values when they aren't expected opens door to unnecessary problems, IMO.

Quoted 5 lines

How about apply the v2 or v3 firstly to fix the virtio_gpu_create_mapping_iov() block issue in virtio-gpu?

I will create another thread for the CHECK(!ret, cmd); thing in rutabaga.

There was a precedent of virtio-gpu not handling positive error codes properly [1]. To me there is no problem that needs to be fixed when virtio_gpu_create_mapping_iov() is never expected to return positive values and doesn't return them.

[1]
https://lore.kernel.org/qemu-devel/20240129073921.446869-1-
[email protected]/

Quoted 5 lines

It's a common expectation that errors are negative. But in practice it's not always true, especially when interacting with external code.

Functionally this patch doesn't change anything. Will leave to Alex and Akihiko to decide on it.

I'm fine either way.

This is a "trade-off" scenario where both two options have upsides and downsides. I often avoid making an argument in that case because it tends to be subjective and cannot be confirmed, leading to bikeshedding.

In my opinion, such a function should not return int in the first place, but should return bool. The wide range of values int can take causes problems; bool allows compilers to enforce that it only returns two values.

Sign is one of those problems. int leads to a mistake like returning errno instead of -errno.

Functions may have completely different ideas for integer return values. In case of virtio-gpu, virtio_gpu_create_mapping_iov() returns -1 but virtio_gpu_update_dmabuf() returns -EINVAL. This is confusing.

Sometimes it is still preferred to keep functions returning int for consistency, but looking at include/hw/virtio/virtio-gpu.h, three return bool and three return int, so returning int here does not improve consistency.

Quoted 4 lines

That all said, this is a bug fix, not refactoring. This patch does fix a bug and avoids polluting the codebase, so it is fine for me. Perhaps it may be nice to have a patch to convert all functions returning int to bool to avoid pitfalls and improve consistency, but that can be done later.

Let's settle with the current variant, I'm okay with it now.

Signature · 2 lines
Best regards,
Dmitry
#11 lore raw
In reply to Dmitry Osipenko
Quoted 49 lines · Dmitry Osipenko
Quoted 38 lines · Honglei Huang
Quoted 30 lines · Dmitry Osipenko
Quoted 21 lines · Markus Armbruster
Quoted 10 lines · Dmitry Osipenko
Quoted 9 lines · Honglei Huang
hw/display/virtio- renamed from hw/display/virtio-gpu-rutabaga.c +0 -0

gpu-rutabaga.c
index ed5ae52acb..ea2928b706 100644

hw/display/virtio-gpu-rutabaga.c +0 -0
@@ -466,7 +466,7 @@ rutabaga_cmd_attach_backing(VirtIOGPU *g,
 struct virtio_gpu_ctrl_command *cmd)
         ret = virtio_gpu_create_mapping_iov(g, att_rb.nr_entries,
 sizeof(att_rb),
Quoted 8 lines

hw/display/virtio-gpu-rutabaga.c rutabaga_cmd_attach_backing(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd) · line 469

Quoted 4 lines
hw/display/virtio-gpu-rutabaga.c +1 -1
                                           cmd, NULL, &res->iov,
 &res->iov_cnt);
-    CHECK(!ret, cmd);
+    CHECK(ret >= 0, cmd);

virtio_gpu_create_mapping_iov() doesn't return positive values, don't see how this change improves anything. You now saying that ret > 0 is okay, while it shall never happen.

Quoted 7 lines

Please see

     Subject: Re: [PATCH] virtio-gpu-virgl: fix error handling in
virgl_cmd_resource_create_blob
     Date: Mon, 17 Nov 2025 08:49:42 +0100
     Message-ID: <[email protected]>
     https://lore.kernel.org/qemu-devel/[email protected]/

Quoted 4 lines

It's a rather common bug when errno isn't negated by mistake and a positive error code is returned. Ignoring positive values when they aren't expected opens door to unnecessary problems, IMO.

Quoted 4 lines

How about apply the v2 or v3 firstly to fix the virtio_gpu_create_mapping_iov() block issue in virtio-gpu?

I will create another thread for the CHECK(!ret, cmd); thing in rutabaga.

There was a precedent of virtio-gpu not handling positive error codes properly [1]. To me there is no problem that needs to be fixed when virtio_gpu_create_mapping_iov() is never expected to return positive values and doesn't return them.

Quoted 9 lines

[1]
https://lore.kernel.org/qemu-devel/[email protected]/

It's a common expectation that errors are negative. But in practice it's not always true, especially when interacting with external code.

Functionally this patch doesn't change anything. Will leave to Alex and Akihiko to decide on it.

Hi Dmitry,

Totally agreed with you.
And actually the origin purpose of this patch is to fix virtio-gpu not
handling error codes properly. Please see the V2: [1].
The error handling logic was incorrect. virtio_gpu_create_mapping_iov()
returns 0 on success and non-zero on failure, but the code was checking whether to set the error response.

The fix changes the condition from 'if (!ret)' to 'if (ret != 0)' to properly handle the return value, consistent with other usage patterns in the same codebase (see virtio-gpu.c:932 and virtio-gpu.c:354).

So I am suggesting to apply the V2.

[1]: https://lore.kernel.org/qemu-devel/[email protected]/

v1: https://lore.kernel.org/qemu-devel/[email protected]/

Regards,
Honglei

#12 lore raw
In reply to Dmitry Osipenko
Quoted 49 lines · Dmitry Osipenko
Quoted 38 lines · Honglei Huang
Quoted 30 lines · Dmitry Osipenko
Quoted 21 lines · Markus Armbruster
Quoted 10 lines · Dmitry Osipenko
Quoted 9 lines · Honglei Huang
hw/display/virtio- renamed from hw/display/virtio-gpu-rutabaga.c +0 -0

gpu-rutabaga.c
index ed5ae52acb..ea2928b706 100644

hw/display/virtio-gpu-rutabaga.c +0 -0
@@ -466,7 +466,7 @@ rutabaga_cmd_attach_backing(VirtIOGPU *g,
 struct virtio_gpu_ctrl_command *cmd)
         ret = virtio_gpu_create_mapping_iov(g, att_rb.nr_entries,
 sizeof(att_rb),
Quoted 8 lines

hw/display/virtio-gpu-rutabaga.c rutabaga_cmd_attach_backing(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd) · line 469

Quoted 4 lines
hw/display/virtio-gpu-rutabaga.c +1 -1
                                           cmd, NULL, &res->iov,
 &res->iov_cnt);
-    CHECK(!ret, cmd);
+    CHECK(ret >= 0, cmd);

virtio_gpu_create_mapping_iov() doesn't return positive values, don't see how this change improves anything. You now saying that ret > 0 is okay, while it shall never happen.

Quoted 7 lines

Please see

     Subject: Re: [PATCH] virtio-gpu-virgl: fix error handling in virgl_cmd_resource_create_blob
     Date: Mon, 17 Nov 2025 08:49:42 +0100
     Message-ID: <[email protected]>
     https://lore.kernel.org/qemu-devel/[email protected]/

Quoted 4 lines

It's a rather common bug when errno isn't negated by mistake and a positive error code is returned. Ignoring positive values when they aren't expected opens door to unnecessary problems, IMO.

Quoted 4 lines

How about apply the v2 or v3 firstly to fix the virtio_gpu_create_mapping_iov() block issue in virtio-gpu?

I will create another thread for the CHECK(!ret, cmd); thing in rutabaga.

There was a precedent of virtio-gpu not handling positive error codes properly [1]. To me there is no problem that needs to be fixed when virtio_gpu_create_mapping_iov() is never expected to return positive values and doesn't return them.

Quoted 8 lines

[1]
https://lore.kernel.org/qemu-devel/[email protected]/

It's a common expectation that errors are negative. But in practice it's not always true, especially when interacting with external code.

Functionally this patch doesn't change anything. Will leave to Alex and Akihiko to decide on it.

Alright, I changed my mind after just typing a code where a fact of error occurrence needs to be propagated. For a code that is internal to QEMU, it should be fine to check for err < 0 because static analysis tools should catch invalid error handling and using same style makes code look more consistent and pleasant to read.

For errors originated externally, I'll continue to advocate for checking of both pos and neg values.

Signature · 2 lines
Best regards,
Dmitry