From: "Liam R. Howlett (Google)" <liam@infradead.org>
To: "Michael S . Tsirkin" <mst@redhat.com>,
"Jason Wang" <jasowangio@gmail.com>,
"Eugenio Pérez" <eperezma@redhat.com>
Cc: "Xuan Zhuo" <xuanzhuo@linux.alibaba.com>,
"Uwe Kleine-König" <u.kleine-koenig@baylibre.com>,
virtualization@lists.linux.dev, linux-kernel@vger.kernel.org,
"Liam R. Howlett (Google)" <liam@infradead.org>
Subject: [PATCH 2/2] tools/virtio: check posix_memalign() for failure
Date: Mon, 19 Oct 2026 23:29:58 -0400 [thread overview]
Message-ID: <20261020032958.1930203-3-liam@infradead.org> (raw)
In-Reply-To: <20261020032958.1930203-1-liam@infradead.org>
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.
Signed-off-by: Liam R. Howlett (Google) <liam@infradead.org>
---
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
next prev parent reply other threads:[~2026-10-20 3:30 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-20 3:29 [PATCH 0/2] tools/virtio: fix the build and the posix_memalign() checks Liam R. Howlett (Google)
2026-10-20 3:29 ` [PATCH 1/2] tools/virtio: add a shim for linux/device-id/virtio.h Liam R. Howlett (Google)
2026-10-20 8:16 ` sashiko-bot
2026-10-20 3:29 ` Liam R. Howlett (Google) [this message]
2026-10-20 8:18 ` [PATCH 2/2] tools/virtio: check posix_memalign() for failure sashiko-bot
2026-10-21 9:44 ` Eugenio Perez Martin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261020032958.1930203-3-liam@infradead.org \
--to=liam@infradead.org \
--cc=eperezma@redhat.com \
--cc=jasowangio@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mst@redhat.com \
--cc=u.kleine-koenig@baylibre.com \
--cc=virtualization@lists.linux.dev \
--cc=xuanzhuo@linux.alibaba.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.