Skip to contentSkip to conversation

tools/virtio: fix the build and the posix_memalign() checks

PATCH In review Liam R. Howlett (Google) 3 participants 6 messages Oct 20 – Oct 21 virtio core

lore
Share & tools
lore
#0 lore raw

cover tools/virtio: fix the build and the posix_memalign() checks

Working on other tools, I came across the fact that posix_memalign()'s return is not the standard negative error. Scanning the git repo, I came across the virtio issue with the return. Testing turned out the be broke as well, so there's a patch to fix testing prior to fixing the caller.

Liam R. Howlett (Google) (2):
tools/virtio: add a shim for linux/device-id/virtio.h tools/virtio: check posix_memalign() for failure

 tools/virtio/linux/device-id/virtio.h | 2 ++
 tools/virtio/linux/kernel.h           | 3 ++-
 tools/virtio/vhost_net_test.c         | 2 +-
 tools/virtio/virtio_test.c            | 2 +-
 4 files changed, 6 insertions(+), 3 deletions(-)
 create mode 100644 tools/virtio/linux/device-id/virtio.h
Signature · 1 line
2.47.3
#1 lore raw

PATCH 1/2 tools/virtio: add a shim for linux/device-id/virtio.h

include/linux/virtio.h takes struct virtio_device_id from <linux/device-id/virtio.h>, which the tools build cannot find.

Add a shim for it beside the others, pointing at the real header.

Fixes ecca1d63c1ea ("Replace <linux/mod_devicetable.h> by more specific <linux/device-id/*.h> (headers)")
---
 tools/virtio/linux/device-id/virtio.h | 2 ++
 1 file changed, 2 insertions(+)
 create mode 100644 tools/virtio/linux/device-id/virtio.h
tools/virtio/linux/device-id/virtio.h new file +2 -0
new file mode 100644
index 0000000000000..3c3e8c514c8d4
@@ -0,0 +1,2 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#include <../../include/linux/device-id/virtio.h>
Signature · 1 line
2.47.3
#2 lore raw

Re: [PATCH 1/2] tools/virtio: add a shim for linux/device-id/virtio.h

Sashiko has reviewed this patch and found no issues.
Quoted 7 lines

include/linux/virtio.h takes struct virtio_device_id from <linux/device-id/virtio.h>, which the tools build cannot find.

Add a shim for it beside the others, pointing at the real header.

Fixes ecca1d63c1ea ("Replace <linux/mod_devicetable.h> by more specific <linux/device-id/*.h> (headers)")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

PATCH 2/2 tools/virtio: check posix_memalign() for failure

In reply to Liam R. Howlett (Google)

posix_memalign() returns zero or a positive error number and does not set errno. virtio_test and vhost_net_test assert that the return is not negative, which will always be true, and the userspace __get_free_page() ignores it, so a failed allocation continues without setting the pointer

Assert a zero return in the tests, and return 0 from __get_free_page() on failure, as the kernel's does.

---
 tools/virtio/linux/kernel.h   | 3 ++-
 tools/virtio/vhost_net_test.c | 2 +-
 tools/virtio/virtio_test.c    | 2 +-
 3 files changed, 4 insertions(+), 3 deletions(-)
tools/virtio/linux/kernel.h +2 -1
index 104abf9d1aee5..3497660c690a9 100644
@@ -106,7 +106,8 @@ static inline unsigned long __get_free_page(gfp_t gfp)
 {
 	void *p;
 
-	posix_memalign(&p, PAGE_SIZE, PAGE_SIZE);
+	if (posix_memalign(&p, PAGE_SIZE, PAGE_SIZE))
+		return 0;
 	return (unsigned long)p;
 }
 
tools/virtio/vhost_net_test.c +1 -1
index 566e15420bb65..3122ea0970568 100644
@@ -197,7 +197,7 @@ static void vq_info_add(struct vdev_info *dev, int idx, int num, int fd)
 	info->idx = idx;
 	info->kick = eventfd(0, EFD_NONBLOCK);
 	r = posix_memalign(&info->ring, 4096, vring_size(num, 4096));
-	assert(r >= 0);
+	assert(!r);
 	vq_reset(info, num, &dev->vdev);
 	vhost_vq_setup(dev, info);
 
tools/virtio/virtio_test.c +1 -1
index 028f54e6854a8..9d61463f6e09b 100644
@@ -116,7 +116,7 @@ static void vq_info_add(struct vdev_info *dev, int num)
 	info->kick = eventfd(0, EFD_NONBLOCK);
 	info->call = eventfd(0, EFD_NONBLOCK);
 	r = posix_memalign(&info->ring, 4096, vring_size(num, 4096));
-	assert(r >= 0);
+	assert(!r);
 	vq_reset(info, num, &dev->vdev);
 	vhost_vq_setup(dev, info);
 	dev->fds[info->idx].fd = info->call;
Signature · 1 line
2.47.3
#4 lore raw

Re: [PATCH 2/2] tools/virtio: check posix_memalign() for failure

Sashiko has reviewed this patch and found no issues.
Quoted 9 lines

posix_memalign() returns zero or a positive error number and does not set errno. virtio_test and vhost_net_test assert that the return is not negative, which will always be true, and the userspace __get_free_page() ignores it, so a failed allocation continues without setting the pointer

Assert a zero return in the tests, and return 0 from __get_free_page() on failure, as the kernel's does.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
maintainer Acked-by on v1 2/2
#5 lore raw

Re: [PATCH 2/2] tools/virtio: check posix_memalign() for failure

In reply to Liam R. Howlett (Google)
Quoted 9 lines · Liam R. Howlett (Google)

posix_memalign() returns zero or a positive error number and does not set errno. virtio_test and vhost_net_test assert that the return is not negative, which will always be true, and the userspace __get_free_page() ignores it, so a failed allocation continues without setting the pointer

Assert a zero return in the tests, and return 0 from __get_free_page() on failure, as the kernel's does.

Thanks!

Quoted 51 lines
---
 tools/virtio/linux/kernel.h   | 3 ++-
 tools/virtio/vhost_net_test.c | 2 +-
 tools/virtio/virtio_test.c    | 2 +-
 3 files changed, 4 insertions(+), 3 deletions(-)
tools/virtio/linux/kernel.h +2 -1
index 104abf9d1aee5..3497660c690a9 100644
@@ -106,7 +106,8 @@ static inline unsigned long __get_free_page(gfp_t gfp)
 {
        void *p;
 
-       posix_memalign(&p, PAGE_SIZE, PAGE_SIZE);
+       if (posix_memalign(&p, PAGE_SIZE, PAGE_SIZE))
+               return 0;
        return (unsigned long)p;
 }
 
tools/virtio/vhost_net_test.c +1 -1
index 566e15420bb65..3122ea0970568 100644
@@ -197,7 +197,7 @@ static void vq_info_add(struct vdev_info *dev, int idx, int num, int fd)
        info->idx = idx;
        info->kick = eventfd(0, EFD_NONBLOCK);
        r = posix_memalign(&info->ring, 4096, vring_size(num, 4096));
-       assert(r >= 0);
+       assert(!r);
        vq_reset(info, num, &dev->vdev);
        vhost_vq_setup(dev, info);
 
tools/virtio/virtio_test.c +1 -2
index 028f54e6854a8..9d61463f6e09b 100644
@@ -116,7 +116,7 @@ static void vq_info_add(struct vdev_info *dev, int num)
        info->kick = eventfd(0, EFD_NONBLOCK);
        info->call = eventfd(0, EFD_NONBLOCK);
        r = posix_memalign(&info->ring, 4096, vring_size(num, 4096));
-       assert(r >= 0);
+       assert(!r);
        vq_reset(info, num, &dev->vdev);
        vhost_vq_setup(dev, info);
        dev->fds[info->idx].fd = info->call;
--

2.47.3