Skip to contentSkip to conversation

vhost-user: fix shared object return values

PATCH Superseded Albert Esteve 3 participants 10 messages Oct 16, 2024 – Oct 21, 2024

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vhost-user: fix shared object return values

VHOST_USER_BACKEND_SHARED_OBJECT_ADD and VHOST_USER_BACKEND_SHARED_OBJECT_REMOVE state in the spec that they return 0 for successful operations, non-zero otherwise. However, implementation relies on the return types of the virtio-dmabuf library, with opposite semantics (true if everything is correct, false otherwise). Therefore, current implementaion violates the specification.

Revert the logic so that the implementation of the vhost-user handling methods matches the specification.

Fixes 043e127a126bb3ceb5fc753deee27d261fd0c5ce
Fixes 160947666276c5b7f6bca4d746bcac2966635d79
---
 hw/virtio/vhost-user.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
hw/virtio/vhost-user.c +4 -4
index 00561daa06..90917352a4 100644
@@ -1607,7 +1607,7 @@ vhost_user_backend_handle_shared_object_add(struct vhost_dev *dev,
     QemuUUID uuid;
 
     memcpy(uuid.data, object->uuid, sizeof(object->uuid));
-    return virtio_add_vhost_device(&uuid, dev);
+    return !virtio_add_vhost_device(&uuid, dev);
 }
 
 static int
@@ -1623,16 +1623,16 @@ vhost_user_backend_handle_shared_object_remove(struct vhost_dev *dev,
         struct vhost_dev *owner = virtio_lookup_vhost_device(&uuid);
         if (dev != owner) {
             /* Not allowed to remove non-owned entries */
-            return 0;
+            return -EPERM;
         }
         break;
     }
     default:
         /* Not allowed to remove non-owned entries */
-        return 0;
+        return -EPERM;
     }
 
-    return virtio_remove_resource(&uuid);
+    return !virtio_remove_resource(&uuid);
 }
 
 static bool vhost_user_send_resp(QIOChannel *ioc, VhostUserHeader *hdr,
Signature · 1 line
2.46.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Albert Esteve

VHOST_USER_BACKEND_SHARED_OBJECT_ADD and VHOST_USER_BACKEND_SHARED_OBJECT_REMOVE state in the spec that they return 0 for successful operations, non-zero otherwise. However, implementation relies on the return types of the virtio-dmabuf library, with opposite semantics (true if everything is correct, false otherwise). Therefore, current implementaion

s/implementaion/implementation

I hadn't seen it ;-P found with:
./scripts/checkpatch.pl --strict --branch master..HEAD --codespell

Quoted 7 lines

violates the specification.

Revert the logic so that the implementation of the vhost-user handling methods matches the specification.

Fixes 043e127a126bb3ceb5fc753deee27d261fd0c5ce

This is in from 9.0 ...

Quoted 1 line
Fixes 160947666276c5b7f6bca4d746bcac2966635d79

... and this from 8.2, so should we consider stable branches?

I think it depends if the backends are checking that return value.

Quoted 4 lines

---
hw/virtio/vhost-user.c | 8 ++++----

1 file changed, 4 insertions(+), 4 deletions(-)

Thanks for the fix!

Quoted 37 lines
hw/virtio/vhost-user.c +1 -1
index 00561daa06..90917352a4 100644
@@ -1607,7 +1607,7 @@ vhost_user_backend_handle_shared_object_add(struct vhost_dev *dev,
    QemuUUID uuid;
 
    memcpy(uuid.data, object->uuid, sizeof(object->uuid));
-    return virtio_add_vhost_device(&uuid, dev);
+    return !virtio_add_vhost_device(&uuid, dev);
 }
 

static int

hw/virtio/vhost-user.c +3 -3
@@ -1623,16 +1623,16 @@ vhost_user_backend_handle_shared_object_remove(struct vhost_dev *dev,
        struct vhost_dev *owner = virtio_lookup_vhost_device(&uuid);
        if (dev != owner) {
            /* Not allowed to remove non-owned entries */
-            return 0;
+            return -EPERM;
        }
        break;
    }
    default:
        /* Not allowed to remove non-owned entries */
-        return 0;
+        return -EPERM;
    }
 
-    return virtio_remove_resource(&uuid);
+    return !virtio_remove_resource(&uuid);
 }
 
 static bool vhost_user_send_resp(QIOChannel *ioc, VhostUserHeader *hdr,
Signature · 1 line
2.46.1
#2 lore raw

Albert Esteve

Senior Software Engineer

Red Hat

[email protected]

Quoted 11 lines · Stefano Garzarella
Quoted 8 lines · Albert Esteve

VHOST_USER_BACKEND_SHARED_OBJECT_ADD and VHOST_USER_BACKEND_SHARED_OBJECT_REMOVE state in the spec that they return 0 for successful operations, non-zero otherwise. However, implementation relies on the return types of the virtio-dmabuf library, with opposite semantics (true if everything is correct, false otherwise). Therefore, current implementaion

Quoted 4 lines

s/implementaion/implementation

I hadn't seen it ;-P found with:
./scripts/checkpatch.pl --strict --branch master..HEAD --codespell

Never used the checkpatch script for spelling. Thanks!

Quoted 7 lines
Quoted 7 lines

violates the specification.

Revert the logic so that the implementation of the vhost-user handling methods matches the specification.

Quoted 7 lines
Quoted 1 line
Fixes 043e127a126bb3ceb5fc753deee27d261fd0c5ce

This is in from 9.0 ...

Quoted 1 line
Fixes 160947666276c5b7f6bca4d746bcac2966635d79

... and this from 8.2, so should we consider stable branches?

You mean in addition to the commits already reflected here?

Quoted 2 lines

I think it depends if the backends are checking that return value.

The return value is optional (requires VHOST_USER_NEED_REPLY), and I am not aware of any backend using this feature so far, in general. So iiuc, that'd mean no need to include stable, right?

Best,
Albert.

Quoted 48 lines
Quoted 5 lines

---
hw/virtio/vhost-user.c | 8 ++++----

1 file changed, 4 insertions(+), 4 deletions(-)

Thanks for the fix!

Quoted 37 lines
hw/virtio/vhost-user.c +1 -1
index 00561daa06..90917352a4 100644
@@ -1607,7 +1607,7 @@ vhost_user_backend_handle_shared_object_add(struct vhost_dev *dev,
    QemuUUID uuid;
 
    memcpy(uuid.data, object->uuid, sizeof(object->uuid));
-    return virtio_add_vhost_device(&uuid, dev);
+    return !virtio_add_vhost_device(&uuid, dev);
 }
 

static int

hw/virtio/vhost-user.c +3 -4
@@ -1623,16 +1623,16 @@ vhost_user_backend_handle_shared_object_remove(struct vhost_dev *dev,
        struct vhost_dev *owner = virtio_lookup_vhost_device(&uuid);
        if (dev != owner) {
            /* Not allowed to remove non-owned entries */
-            return 0;
+            return -EPERM;
        }
        break;
    }
    default:
        /* Not allowed to remove non-owned entries */
-        return 0;
+        return -EPERM;
    }
 
-    return virtio_remove_resource(&uuid);
+    return !virtio_remove_resource(&uuid);
 }
 
 static bool vhost_user_send_resp(QIOChannel *ioc, VhostUserHeader *hdr,
--

2.46.1

#3 lore raw
Quoted 38 lines · Albert Esteve

Albert Esteve

Senior Software Engineer

Red Hat

[email protected]

Quoted 11 lines · Stefano Garzarella
Quoted 8 lines · Albert Esteve

VHOST_USER_BACKEND_SHARED_OBJECT_ADD and VHOST_USER_BACKEND_SHARED_OBJECT_REMOVE state in the spec that they return 0 for successful operations, non-zero otherwise. However, implementation relies on the return types of the virtio-dmabuf library, with opposite semantics (true if everything is correct, false otherwise). Therefore, current implementaion

Quoted 4 lines

s/implementaion/implementation

I hadn't seen it ;-P found with:
./scripts/checkpatch.pl --strict --branch master..HEAD --codespell

Never used the checkpatch script for spelling. Thanks!

Quoted 9 lines
Quoted 8 lines

violates the specification.

Revert the logic so that the implementation of the vhost-user handling methods matches the specification.

Fixes 043e127a126bb3ceb5fc753deee27d261fd0c5ce
Quoted 7 lines
Quoted 5 lines

This is in from 9.0 ...

Quoted 1 line
Fixes 160947666276c5b7f6bca4d746bcac2966635d79

... and this from 8.2, so should we consider stable branches?

You mean in addition to the commits already reflected here?

Nope, I just mean if we need to cc [email protected] in order to backport this patch on stable branches. See docs/devel/stable-process.rst

Quoted 7 lines
Quoted 3 lines

I think it depends if the backends are checking that return value.

The return value is optional (requires VHOST_USER_NEED_REPLY), and I am not aware of any backend using this feature so far, in general. So iiuc, that'd mean no need to include stable, right?

Yep, if no one uses it, we can avoid it for now. On the other hand if the patch is simple, perhaps it might make sense to avoid future issues.

Michael WDYT?

Thanks
Stefano

Quoted 53 lines

Best,
Albert.

Quoted 48 lines
Quoted 5 lines

---
hw/virtio/vhost-user.c | 8 ++++----

1 file changed, 4 insertions(+), 4 deletions(-)

Thanks for the fix!

Quoted 37 lines
hw/virtio/vhost-user.c +1 -1
index 00561daa06..90917352a4 100644
@@ -1607,7 +1607,7 @@ vhost_user_backend_handle_shared_object_add(struct vhost_dev *dev,
    QemuUUID uuid;
 
    memcpy(uuid.data, object->uuid, sizeof(object->uuid));
-    return virtio_add_vhost_device(&uuid, dev);
+    return !virtio_add_vhost_device(&uuid, dev);
 }
 

static int

hw/virtio/vhost-user.c +3 -4
@@ -1623,16 +1623,16 @@ vhost_user_backend_handle_shared_object_remove(struct vhost_dev *dev,
        struct vhost_dev *owner = virtio_lookup_vhost_device(&uuid);
        if (dev != owner) {
            /* Not allowed to remove non-owned entries */
-            return 0;
+            return -EPERM;
        }
        break;
    }
    default:
        /* Not allowed to remove non-owned entries */
-        return 0;
+        return -EPERM;
    }
 
-    return virtio_remove_resource(&uuid);
+    return !virtio_remove_resource(&uuid);
 }
 
 static bool vhost_user_send_resp(QIOChannel *ioc, VhostUserHeader *hdr,
--

2.46.1

#4 lore raw
Quoted 44 lines · Stefano Garzarella
Quoted 42 lines · Albert Esteve

Albert Esteve

Senior Software Engineer

Red Hat

[email protected]

Quoted 11 lines · Stefano Garzarella
Quoted 8 lines · Albert Esteve

VHOST_USER_BACKEND_SHARED_OBJECT_ADD and VHOST_USER_BACKEND_SHARED_OBJECT_REMOVE state in the spec that they return 0 for successful operations, non-zero otherwise. However, implementation relies on the return types of the virtio-dmabuf library, with opposite semantics (true if everything is correct, false otherwise). Therefore, current implementaion

Quoted 4 lines

s/implementaion/implementation

I hadn't seen it ;-P found with:
./scripts/checkpatch.pl --strict --branch master..HEAD --codespell

Never used the checkpatch script for spelling. Thanks!

Quoted 13 lines
Quoted 8 lines

violates the specification.

Revert the logic so that the implementation of the vhost-user handling methods matches the specification.

Fixes 043e127a126bb3ceb5fc753deee27d261fd0c5ce

This is in from 9.0 ...

Quoted 1 line
Fixes 160947666276c5b7f6bca4d746bcac2966635d79
Quoted 7 lines
Quoted 3 lines
Quoted 1 line

... and this from 8.2, so should we consider stable branches?

You mean in addition to the commits already reflected here?

Nope, I just mean if we need to cc [email protected] in order to backport this patch on stable branches.
See docs/devel/stable-process.rst

Got it, thanks!

Quoted 11 lines
Quoted 8 lines
Quoted 4 lines

I think it depends if the backends are checking that return value.

The return value is optional (requires VHOST_USER_NEED_REPLY), and I am not aware of any backend using this feature so far, in general. So iiuc, that'd mean no need to include stable, right?

Yep, if no one uses it, we can avoid it for now. On the other hand if the patch is simple, perhaps it might make sense to avoid future issues.

I will wait until tomorrow and post a new version of the patch fixing the typo and changing the return as suggested by Daniel.
The patch is simple, so it could make sense to include stable. But in principle, I understood that I can avoid stable for now. If we decide otherwise and I have already sent v2, we can add them a posteriori I guess.

BR,
Albert.

Quoted 60 lines

Michael WDYT?

Thanks
Stefano

Quoted 53 lines

Best,
Albert.

Quoted 48 lines
Quoted 5 lines

---
hw/virtio/vhost-user.c | 8 ++++----

1 file changed, 4 insertions(+), 4 deletions(-)

Thanks for the fix!

Quoted 37 lines
hw/virtio/vhost-user.c +1 -1
index 00561daa06..90917352a4 100644
@@ -1607,7 +1607,7 @@ vhost_user_backend_handle_shared_object_add(struct vhost_dev *dev,
    QemuUUID uuid;
 
    memcpy(uuid.data, object->uuid, sizeof(object->uuid));
-    return virtio_add_vhost_device(&uuid, dev);
+    return !virtio_add_vhost_device(&uuid, dev);
 }
 

static int

hw/virtio/vhost-user.c +3 -4
@@ -1623,16 +1623,16 @@ vhost_user_backend_handle_shared_object_remove(struct vhost_dev *dev,
        struct vhost_dev *owner = virtio_lookup_vhost_device(&uuid);
        if (dev != owner) {
            /* Not allowed to remove non-owned entries */
-            return 0;
+            return -EPERM;
        }
        break;
    }
    default:
        /* Not allowed to remove non-owned entries */
-        return 0;
+        return -EPERM;
    }
 
-    return virtio_remove_resource(&uuid);
+    return !virtio_remove_resource(&uuid);
 }
 
 static bool vhost_user_send_resp(QIOChannel *ioc, VhostUserHeader *hdr,
--

2.46.1

#5 lore raw
In reply to Albert Esteve
Quoted 26 lines · Albert Esteve

VHOST_USER_BACKEND_SHARED_OBJECT_ADD and VHOST_USER_BACKEND_SHARED_OBJECT_REMOVE state in the spec that they return 0 for successful operations, non-zero otherwise. However, implementation relies on the return types of the virtio-dmabuf library, with opposite semantics (true if everything is correct, false otherwise). Therefore, current implementaion violates the specification.

Revert the logic so that the implementation of the vhost-user handling methods matches the specification.

Fixes 043e127a126bb3ceb5fc753deee27d261fd0c5ce
Fixes 160947666276c5b7f6bca4d746bcac2966635d79
---
 hw/virtio/vhost-user.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
hw/virtio/vhost-user.c +0 -0
index 00561daa06..90917352a4 100644

hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_add(struct vhost_dev *dev, · line 1611

Quoted 6 lines
hw/virtio/vhost-user.c +1 -1
@@ -1607,7 +1607,7 @@ vhost_user_backend_handle_shared_object_add(struct vhost_dev *dev,
     QemuUUID uuid;
 
     memcpy(uuid.data, object->uuid, sizeof(object->uuid));
-    return virtio_add_vhost_device(&uuid, dev);
+    return !virtio_add_vhost_device(&uuid, dev);
 }

This virtio_add_vhost_device() method returns a bool, but this vhost_user_backend_handle_shared_object_add() method returns an int, but fills that int with an inverted bool value. The caller then assigns the return value to an int, but then interprets the int as a bool, and assigns that bool result to an u64.

This call chain is madness :-(

Change vhost_user_backend_handle_shared_object_add to return a bool to reduce the madness IMHO.

Quoted 10 lines
hw/virtio/vhost-user.c +1 -1
 static int
@@ -1623,16 +1623,16 @@ vhost_user_backend_handle_shared_object_remove(struct vhost_dev *dev,
         struct vhost_dev *owner = virtio_lookup_vhost_device(&uuid);
         if (dev != owner) {
             /* Not allowed to remove non-owned entries */
-            return 0;
+            return -EPERM;
         }
         break;

hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_remove(struct vhost_dev *dev, · line 1636

Quoted 10 lines
hw/virtio/vhost-user.c +2 -2
     }
     default:
         /* Not allowed to remove non-owned entries */
-        return 0;
+        return -EPERM;
     }
 
-    return virtio_remove_resource(&uuid);
+    return !virtio_remove_resource(&uuid);
 }

These return values are inconsistent.

In some places you're returning a negative errno, but in this last place you're returning true or false, by calling virtio_remove_resource which is a 'bool' method & inverting it.

On top of this inconsistency, it has all the same madness mentioneed above.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#6 lore raw
Quoted 42 lines · Daniel P. Berrangé
Quoted 26 lines · Albert Esteve

VHOST_USER_BACKEND_SHARED_OBJECT_ADD and VHOST_USER_BACKEND_SHARED_OBJECT_REMOVE state in the spec that they return 0 for successful operations, non-zero otherwise. However, implementation relies on the return types of the virtio-dmabuf library, with opposite semantics (true if everything is correct, false otherwise). Therefore, current implementaion violates the specification.

Revert the logic so that the implementation of the vhost-user handling methods matches the specification.

Fixes 043e127a126bb3ceb5fc753deee27d261fd0c5ce
Fixes 160947666276c5b7f6bca4d746bcac2966635d79
---
 hw/virtio/vhost-user.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
hw/virtio/vhost-user.c +0 -0
index 00561daa06..90917352a4 100644

hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_add(struct vhost_dev *dev, · line 1611

Quoted 6 lines
hw/virtio/vhost-user.c +1 -1
@@ -1607,7 +1607,7 @@ vhost_user_backend_handle_shared_object_add(struct vhost_dev *dev,
     QemuUUID uuid;
 
     memcpy(uuid.data, object->uuid, sizeof(object->uuid));
-    return virtio_add_vhost_device(&uuid, dev);
+    return !virtio_add_vhost_device(&uuid, dev);
 }

This virtio_add_vhost_device() method returns a bool, but this vhost_user_backend_handle_shared_object_add() method returns an int, but fills that int with an inverted bool value. The caller then assigns the return value to an int, but then interprets the int as a bool, and assigns that bool result to an u64.

Quoted 1 line

This call chain is madness :-(

TBF most of the madness is part of the already existing handling infrastructure. vhost_user_backend_handle_shared_object_add() returns an int to be consistent with other handling functions.

Quoted 3 lines

Change vhost_user_backend_handle_shared_object_add to return a bool to reduce the madness IMHO.

Changing it to bool would make it inconsistent
wrt other handlers, and the casting would happen nonetheless on assignment. Not sure if that is an improvement.

Quoted 24 lines
Quoted 11 lines
hw/virtio/vhost-user.c +1 -1
 static int
@@ -1623,16 +1623,16 @@ vhost_user_backend_handle_shared_object_remove(struct vhost_dev *dev,
         struct vhost_dev *owner = virtio_lookup_vhost_device(&uuid);
         if (dev != owner) {
             /* Not allowed to remove non-owned entries */
-            return 0;
+            return -EPERM;
         }
         break;

hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_remove(struct vhost_dev *dev, · line 1636

Quoted 10 lines
hw/virtio/vhost-user.c +2 -2
     }
     default:
         /* Not allowed to remove non-owned entries */
-        return 0;
+        return -EPERM;
     }
 
-    return virtio_remove_resource(&uuid);
+    return !virtio_remove_resource(&uuid);
 }

These return values are inconsistent.

Quoted 3 lines

In some places you're returning a negative errno, but in this last place you're returning true or false, by calling virtio_remove_resource which is a 'bool' method & inverting it.

Well, specification only distinguish between zero and non-zero values. But for clarity, I guess I could do something like:

```
if (!virtio_remove_resource(&uuid)) {
    return -EINVAL;
}

return 0;
```

Same for the vhost_user_backend_handle_shared_object_add() handler (in that case there is no inconsistency with positive or negative return values, but still better to maintain similar strategy for all handlers).

BR,
Albert.

Quoted 11 lines

On top of this inconsistency, it has all the same madness mentioneed above.

With regards,
Daniel
--

|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#7 lore raw
Quoted 52 lines · Albert Esteve
Quoted 42 lines · Daniel P. Berrangé
Quoted 26 lines · Albert Esteve

VHOST_USER_BACKEND_SHARED_OBJECT_ADD and VHOST_USER_BACKEND_SHARED_OBJECT_REMOVE state in the spec that they return 0 for successful operations, non-zero otherwise. However, implementation relies on the return types of the virtio-dmabuf library, with opposite semantics (true if everything is correct, false otherwise). Therefore, current implementaion violates the specification.

Revert the logic so that the implementation of the vhost-user handling methods matches the specification.

Fixes 043e127a126bb3ceb5fc753deee27d261fd0c5ce
Fixes 160947666276c5b7f6bca4d746bcac2966635d79
---
 hw/virtio/vhost-user.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
hw/virtio/vhost-user.c +0 -0
index 00561daa06..90917352a4 100644

hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_add(struct vhost_dev *dev, · line 1611

Quoted 6 lines
hw/virtio/vhost-user.c +1 -1
@@ -1607,7 +1607,7 @@ vhost_user_backend_handle_shared_object_add(struct vhost_dev *dev,
     QemuUUID uuid;
 
     memcpy(uuid.data, object->uuid, sizeof(object->uuid));
-    return virtio_add_vhost_device(&uuid, dev);
+    return !virtio_add_vhost_device(&uuid, dev);
 }

This virtio_add_vhost_device() method returns a bool, but this vhost_user_backend_handle_shared_object_add() method returns an int, but fills that int with an inverted bool value. The caller then assigns the return value to an int, but then interprets the int as a bool, and assigns that bool result to an u64.

Quoted 1 line

This call chain is madness :-(

TBF most of the madness is part of the already existing handling infrastructure. vhost_user_backend_handle_shared_object_add() returns an int to be consistent with other handling functions.

Quoted 1 line
Quoted 6 lines
Quoted 2 lines

Change vhost_user_backend_handle_shared_object_add to return a bool to reduce the madness IMHO.

Changing it to bool would make it inconsistent
wrt other handlers, and the casting would happen nonetheless on assignment. Not sure if that is an improvement.

Well when the caller does

        payload.u64 = !!ret;

it is saying that it only cares about the values
being 0 or 1. So how about just making these
methods return 0 or 1 then.

Quoted 39 lines
Quoted 25 lines
Quoted 12 lines
hw/virtio/vhost-user.c +1 -1
 static int
@@ -1623,16 +1623,16 @@ vhost_user_backend_handle_shared_object_remove(struct vhost_dev *dev,
         struct vhost_dev *owner = virtio_lookup_vhost_device(&uuid);
         if (dev != owner) {
             /* Not allowed to remove non-owned entries */
-            return 0;
+            return -EPERM;
         }
         break;

hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_remove(struct vhost_dev *dev, · line 1636

Quoted 10 lines
hw/virtio/vhost-user.c +2 -2
     }
     default:
         /* Not allowed to remove non-owned entries */
-        return 0;
+        return -EPERM;
     }
 
-    return virtio_remove_resource(&uuid);
+    return !virtio_remove_resource(&uuid);
 }

These return values are inconsistent.

Quoted 3 lines

In some places you're returning a negative errno, but in this last place you're returning true or false, by calling virtio_remove_resource which is a 'bool' method & inverting it.

Well, specification only distinguish between zero and non-zero values. But for clarity, I guess I could do something like:

```
if (!virtio_remove_resource(&uuid)) {
    return -EINVAL;
}

return 0;
```
Quoted 4 lines

Same for the vhost_user_backend_handle_shared_object_add() handler (in that case there is no inconsistency with positive or negative return values, but still better to maintain similar strategy for all handlers).

Returning an errno value, when the caller only wants 0 or 1 is pointless.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#8 lore raw
Quoted 61 lines · Daniel P. Berrangé
Quoted 52 lines · Albert Esteve
Quoted 42 lines · Daniel P. Berrangé
Quoted 26 lines · Albert Esteve

VHOST_USER_BACKEND_SHARED_OBJECT_ADD and VHOST_USER_BACKEND_SHARED_OBJECT_REMOVE state in the spec that they return 0 for successful operations, non-zero otherwise. However, implementation relies on the return types of the virtio-dmabuf library, with opposite semantics (true if everything is correct, false otherwise). Therefore, current implementaion violates the specification.

Revert the logic so that the implementation of the vhost-user handling methods matches the specification.

Fixes 043e127a126bb3ceb5fc753deee27d261fd0c5ce
Fixes 160947666276c5b7f6bca4d746bcac2966635d79
---
 hw/virtio/vhost-user.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
hw/virtio/vhost-user.c +0 -0
index 00561daa06..90917352a4 100644

hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_add(struct vhost_dev *dev, · line 1611

Quoted 6 lines
hw/virtio/vhost-user.c +1 -1
@@ -1607,7 +1607,7 @@ vhost_user_backend_handle_shared_object_add(struct vhost_dev *dev,
     QemuUUID uuid;
 
     memcpy(uuid.data, object->uuid, sizeof(object->uuid));
-    return virtio_add_vhost_device(&uuid, dev);
+    return !virtio_add_vhost_device(&uuid, dev);
 }

This virtio_add_vhost_device() method returns a bool, but this vhost_user_backend_handle_shared_object_add() method returns an int, but fills that int with an inverted bool value. The caller then assigns the return value to an int, but then interprets the int as a bool, and assigns that bool result to an u64.

Quoted 1 line

This call chain is madness :-(

TBF most of the madness is part of the already existing handling infrastructure. vhost_user_backend_handle_shared_object_add() returns an int to be consistent with other handling functions.

Quoted 1 line
Quoted 6 lines
Quoted 2 lines

Change vhost_user_backend_handle_shared_object_add to return a bool to reduce the madness IMHO.

Changing it to bool would make it inconsistent
wrt other handlers, and the casting would happen nonetheless on assignment. Not sure if that is an improvement.

Quoted 7 lines

Well when the caller does

        payload.u64 = !!ret;

it is saying that it only cares about the values
being 0 or 1. So how about just making these
methods return 0 or 1 then.

Ah, I see your point. I introduced negative error values just because I saw other handlers doing
it (e.g., vhost_user_backend_handle_vring_host_notifier()).

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_remove(struct vhost_dev *dev, · line 1626

Quoted 11 lines
hw/virtio/vhost-user.c +1 -1
 static int
@@ -1623,16 +1623,16 @@ vhost_user_backend_handle_shared_object_remove(struct vhost_dev *dev,
         struct vhost_dev *owner = virtio_lookup_vhost_device(&uuid);
         if (dev != owner) {
             /* Not allowed to remove non-owned entries */
-            return 0;
+            return -EPERM;

So you are suggesting here it could be return 1; instead? It does not look clear enough that it is an error value.

Maybe I could create a define like:

#define EVHOST_USER 1

and use it here (and probably a good idea to change other handling functions in a different commit, to be consistent). WDYT?

BR,
Albert.

Quoted 44 lines
Quoted 29 lines
Quoted 13 lines

hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_remove(struct vhost_dev *dev, · line 1636

Quoted 12 lines
hw/virtio/vhost-user.c +2 -2
         }
         break;
     }
     default:
         /* Not allowed to remove non-owned entries */
-        return 0;
+        return -EPERM;
     }
 
-    return virtio_remove_resource(&uuid);
+    return !virtio_remove_resource(&uuid);
 }
Quoted 5 lines

These return values are inconsistent.

In some places you're returning a negative errno, but in this last place you're returning true or false, by calling virtio_remove_resource which is a 'bool' method & inverting it.

Well, specification only distinguish between zero and non-zero values. But for clarity, I guess I could do something like:

```
if (!virtio_remove_resource(&uuid)) {
    return -EINVAL;
}

return 0;
```
Quoted 4 lines

Same for the vhost_user_backend_handle_shared_object_add() handler (in that case there is no inconsistency with positive or negative return values, but still better to maintain similar strategy for all handlers).

Returning an errno value, when the caller only wants 0 or 1 is pointless.

With regards,
Daniel
--

|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#9 lore raw
Quoted 74 lines · Albert Esteve
Quoted 61 lines · Daniel P. Berrangé
Quoted 52 lines · Albert Esteve
Quoted 42 lines · Daniel P. Berrangé
Quoted 26 lines · Albert Esteve

VHOST_USER_BACKEND_SHARED_OBJECT_ADD and VHOST_USER_BACKEND_SHARED_OBJECT_REMOVE state in the spec that they return 0 for successful operations, non-zero otherwise. However, implementation relies on the return types of the virtio-dmabuf library, with opposite semantics (true if everything is correct, false otherwise). Therefore, current implementaion violates the specification.

Revert the logic so that the implementation of the vhost-user handling methods matches the specification.

Fixes 043e127a126bb3ceb5fc753deee27d261fd0c5ce
Fixes 160947666276c5b7f6bca4d746bcac2966635d79
---
 hw/virtio/vhost-user.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
hw/virtio/vhost-user.c +0 -0
index 00561daa06..90917352a4 100644

hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_add(struct vhost_dev *dev, · line 1611

Quoted 6 lines
hw/virtio/vhost-user.c +1 -1
@@ -1607,7 +1607,7 @@ vhost_user_backend_handle_shared_object_add(struct vhost_dev *dev,
     QemuUUID uuid;
 
     memcpy(uuid.data, object->uuid, sizeof(object->uuid));
-    return virtio_add_vhost_device(&uuid, dev);
+    return !virtio_add_vhost_device(&uuid, dev);
 }

This virtio_add_vhost_device() method returns a bool, but this vhost_user_backend_handle_shared_object_add() method returns an int, but fills that int with an inverted bool value. The caller then assigns the return value to an int, but then interprets the int as a bool, and assigns that bool result to an u64.

Quoted 1 line

This call chain is madness :-(

TBF most of the madness is part of the already existing handling infrastructure. vhost_user_backend_handle_shared_object_add() returns an int to be consistent with other handling functions.

Quoted 1 line
Quoted 6 lines
Quoted 2 lines

Change vhost_user_backend_handle_shared_object_add to return a bool to reduce the madness IMHO.

Changing it to bool would make it inconsistent
wrt other handlers, and the casting would happen nonetheless on assignment. Not sure if that is an improvement.

Quoted 7 lines

Well when the caller does

        payload.u64 = !!ret;

it is saying that it only cares about the values
being 0 or 1. So how about just making these
methods return 0 or 1 then.

Ah, I see your point. I introduced negative error values just because I saw other handlers doing
it (e.g., vhost_user_backend_handle_vring_host_notifier()).

Quoted 10 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_remove(struct vhost_dev *dev, · line 1626

Quoted 7 lines
hw/virtio/vhost-user.c +1 -1
 static int
@@ -1623,16 +1623,16 @@ vhost_user_backend_handle_shared_object_remove(struct vhost_dev *dev,
         struct vhost_dev *owner = virtio_lookup_vhost_device(&uuid);
         if (dev != owner) {
             /* Not allowed to remove non-owned entries */
-            return 0;
+            return -EPERM;

So you are suggesting here it could be return 1; instead? It does not look clear enough that it is an error value.

Add API documentation comments to these methods

"Returns: 0 on success, 1 on error"

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|