From ec6177dfe98b9be1c3ede6c0dfe4394ea2a76959 Mon Sep 17 00:00:00 2001 From: longlong yan Date: Fri, 5 Jun 2026 10:14:45 +0800 Subject: tools/virtio: check mmap return value in vringh_test In parallel_test(), the return values of mmap() for both host_map and guest_map are not checked against MAP_FAILED. If mmap() fails, the subsequent code will dereference the invalid pointer, leading to a segmentation fault. Add MAP_FAILED checks after both mmap() calls, using err() to report the error and exit, consistent with the existing error handling style in this file (e.g., the open() call on line 149). Fixes: 1515c5ce26ae ("tools/virtio: add vring_test.") Signed-off-by: longlong yan Signed-off-by: Michael S. Tsirkin Message-ID: <20260605021446.1611-1-yanlonglong@kylinos.cn> --- tools/virtio/vringh_test.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'tools') diff --git a/tools/virtio/vringh_test.c b/tools/virtio/vringh_test.c index b9591223437a..5ea6d29bc992 100644 --- a/tools/virtio/vringh_test.c +++ b/tools/virtio/vringh_test.c @@ -159,7 +159,12 @@ static int parallel_test(u64 features, /* Parent and child use separate addresses, to check our mapping logic! */ host_map = mmap(NULL, mapsize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); + if (host_map == MAP_FAILED) + err(1, "mmap host_map"); + guest_map = mmap(NULL, mapsize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); + if (guest_map == MAP_FAILED) + err(1, "mmap guest_map"); pipe_ret = pipe(to_guest); assert(!pipe_ret); -- cgit v1.2.3 From 3afcb3630944d7565d6ab6106b61461b292aa93f Mon Sep 17 00:00:00 2001 From: "Michael S. Tsirkin" Date: Wed, 3 Jun 2026 16:33:28 -0400 Subject: tools/virtio: fix build for kmalloc_obj API and missing stubs Add stubs for kmalloc_obj() and kmalloc_objs() to the tools/virtio test harness, matching the new kernel allocator API. Also add the DMA_ATTR_CPU_CACHE_CLEAN definition and include kernel.h from err.h for the unlikely() macro. Assisted-by: Claude:claude-opus-4-6 Signed-off-by: Michael S. Tsirkin Message-ID: --- tools/virtio/linux/dma-mapping.h | 2 ++ tools/virtio/linux/err.h | 1 + tools/virtio/linux/kernel.h | 6 ++++++ 3 files changed, 9 insertions(+) (limited to 'tools') diff --git a/tools/virtio/linux/dma-mapping.h b/tools/virtio/linux/dma-mapping.h index fddfa2fbb276..8d1a16cb20db 100644 --- a/tools/virtio/linux/dma-mapping.h +++ b/tools/virtio/linux/dma-mapping.h @@ -60,4 +60,6 @@ enum dma_data_direction { */ #define DMA_MAPPING_ERROR (~(dma_addr_t)0) +#define DMA_ATTR_CPU_CACHE_CLEAN (1UL << 11) + #endif diff --git a/tools/virtio/linux/err.h b/tools/virtio/linux/err.h index 0943c644a701..b7b4cb516dc9 100644 --- a/tools/virtio/linux/err.h +++ b/tools/virtio/linux/err.h @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: GPL-2.0 */ #ifndef ERR_H #define ERR_H +#include #define MAX_ERRNO 4095 #define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO) diff --git a/tools/virtio/linux/kernel.h b/tools/virtio/linux/kernel.h index 416d02703f61..104abf9d1aee 100644 --- a/tools/virtio/linux/kernel.h +++ b/tools/virtio/linux/kernel.h @@ -65,6 +65,12 @@ static inline void *kmalloc_array(unsigned n, size_t s, gfp_t gfp) return kmalloc(n * s, gfp); } +#define kmalloc_obj(VAR_OR_TYPE, ...) \ + ((typeof(VAR_OR_TYPE) *)kmalloc(sizeof(typeof(VAR_OR_TYPE)), 0)) + +#define kmalloc_objs(VAR_OR_TYPE, COUNT, ...) \ + ((typeof(VAR_OR_TYPE) *)kmalloc(sizeof(typeof(VAR_OR_TYPE)) * (COUNT), 0)) + static inline void *kzalloc(size_t s, gfp_t gfp) { void *p = kmalloc(s, gfp); -- cgit v1.2.3