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

From: Eugenio Perez Martin

Date: Wed Oct 21 2026 - 05:46:05 EST


On Tue, Oct 20, 2026 at 5:30 AM Liam R. Howlett (Google)
<liam@xxxxxxxxxxxxx> wrote:
>
> 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.
>

Acked-by: Eugenio Pérez <eperezma@xxxxxxxxxx>

Thanks!

> Signed-off-by: Liam R. Howlett (Google) <liam@xxxxxxxxxxxxx>
> ---
> 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(-)
>
> diff --git a/tools/virtio/linux/kernel.h b/tools/virtio/linux/kernel.h
> index 104abf9d1aee5..3497660c690a9 100644
> --- a/tools/virtio/linux/kernel.h
> +++ b/tools/virtio/linux/kernel.h
> @@ -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;
> }
>
> diff --git a/tools/virtio/vhost_net_test.c b/tools/virtio/vhost_net_test.c
> index 566e15420bb65..3122ea0970568 100644
> --- a/tools/virtio/vhost_net_test.c
> +++ b/tools/virtio/vhost_net_test.c
> @@ -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);
>
> diff --git a/tools/virtio/virtio_test.c b/tools/virtio/virtio_test.c
> index 028f54e6854a8..9d61463f6e09b 100644
> --- a/tools/virtio/virtio_test.c
> +++ b/tools/virtio/virtio_test.c
> @@ -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
>
>