From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: passt.top; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=U4C7aL5c; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by passt.top (Postfix) with ESMTPS id D0CAD5A0279 for ; Fri, 08 Aug 2025 16:02:51 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1754661770; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=kAinnVxwAH6Y1EUeUjJv8OnJ1adxA6L8ZpO2AdbHI3s=; b=U4C7aL5cRLPSiJbjBgmvk7zJa2vV7u5mqLzYe/SmqIORA4pfBM+VnXr9/y0zO5fb9i3W5P 2liOctjq4RcqoCedOkbWlqvaV7kb2YSP46s4WhSFyr5VpaGq8eWvQaROCh0KpO5hDhwEga FTT0kwjSS2XbX/4VG0hAnlE/OzjlJQk= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-619-92OcGejYNYuRJiZGGwyj-g-1; Fri, 08 Aug 2025 10:02:49 -0400 X-MC-Unique: 92OcGejYNYuRJiZGGwyj-g-1 X-Mimecast-MFC-AGG-ID: 92OcGejYNYuRJiZGGwyj-g_1754661768 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 7722019560B3 for ; Fri, 8 Aug 2025 14:02:48 +0000 (UTC) Received: from lenovo-t14s.redhat.com (unknown [10.45.225.81]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 493D01954199; Fri, 8 Aug 2025 14:02:47 +0000 (UTC) From: Laurent Vivier To: passt-dev@passt.top Subject: [PATCH v9 29/30] packet: Refactor vhost-user memory region handling Date: Fri, 8 Aug 2025 16:01:41 +0200 Message-ID: <20250808140142.3404325-30-lvivier@redhat.com> In-Reply-To: <20250808140142.3404325-1-lvivier@redhat.com> References: <20250808140142.3404325-1-lvivier@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 1-kPo-6_9cX3GN_zXRiexjVA1BU3gpPCxTEUngUyCSY_1754661768 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true Message-ID-Hash: QGHV4OUQITYHMV5OT5NTFTHLLUZTVNYR X-Message-ID-Hash: QGHV4OUQITYHMV5OT5NTFTHLLUZTVNYR X-MailFrom: lvivier@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Laurent Vivier X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: This patch refactors the handling of vhost-user memory regions by introducing a new `struct vdev_memory` to encapsulate the regions array and their count (`nregions`) within the main `vu_dev` structure. This new `vdev_memory` structure is then passed to the packet pool by re-using the existing `p->buf` field. A `p->buf_size` of 0 indicates that `p->buf` holds a pointer to `struct vdev_memory` instead of a regular packet buffer. A new helper, `get_vdev_memory()`, is added to abstract this access pattern. Previous implementation was using a marker at the end of the memory regions array. We can now uses all the slots. Signed-off-by: Laurent Vivier --- packet.c | 21 +++++++++++++++++++-- packet.h | 6 ++++-- tap.c | 4 ++-- tap.h | 1 - vhost_user.c | 28 +++++++++++----------------- virtio.c | 4 ++-- virtio.h | 18 ++++++++++++++---- vu_common.c | 22 ++++++++++++---------- 8 files changed, 64 insertions(+), 40 deletions(-) diff --git a/packet.c b/packet.c index cbc43c2fc22d..27693c55a138 100644 --- a/packet.c +++ b/packet.c @@ -22,6 +22,20 @@ #include "util.h" #include "log.h" +/** + * get_vdev_memory() - Return a pointer to the memory regions of the pool + * @p: Packet pool + * + * Return: Null if none, otherwise a pointer to vdev_memory structure + */ +static struct vdev_memory *get_vdev_memory(const struct pool *p) +{ + if (p->buf_size) + return NULL; + + return (struct vdev_memory *)p->buf; +} + /** * packet_check_range() - Check if a memory range is valid for a pool * @p: Packet pool @@ -35,16 +49,19 @@ static int packet_check_range(const struct pool *p, const char *ptr, size_t len, const char *func, int line) { + struct vdev_memory *memory; + if (len > PACKET_MAX_LEN) { debug("packet range length %zu (max %zu), %s:%i", len, PACKET_MAX_LEN, func, line); return -1; } - if (p->buf_size == 0) { + memory = get_vdev_memory(p); + if (memory) { int ret; - ret = vu_packet_check_range((void *)p->buf, ptr, len); + ret = vu_packet_check_range(memory, ptr, len); if (ret == -1) debug("cannot find region, %s:%i", func, line); diff --git a/packet.h b/packet.h index 43b9022075d1..e51cbd19fdc4 100644 --- a/packet.h +++ b/packet.h @@ -8,6 +8,7 @@ #include #include "iov.h" +#include "virtio.h" /* Maximum size of a single packet stored in pool, including headers */ #define PACKET_MAX_LEN ((size_t)UINT16_MAX) @@ -15,7 +16,7 @@ /** * struct pool - Generic pool of packets stored in a buffer * @buf: Buffer storing packet descriptors, - * a struct vu_dev_region array for passt vhost-user mode + * a struct vdev_region for passt vhost-user mode * @buf_size: Total size of buffer, * 0 for passt vhost-user mode * @size: Number of usable descriptors for the pool @@ -30,7 +31,8 @@ struct pool { struct iovec pkt[]; }; -int vu_packet_check_range(void *buf, const char *ptr, size_t len); +int vu_packet_check_range(struct vdev_memory *memory, + const char *ptr, size_t len); void packet_add_do(struct pool *p, struct iov_tail *data, const char *func, int line); bool packet_get_do(const struct pool *p, const size_t idx, diff --git a/tap.c b/tap.c index bbc786468455..9fd00915bb01 100644 --- a/tap.c +++ b/tap.c @@ -1458,7 +1458,7 @@ static void tap_sock_tun_init(struct ctx *c) * @base: Buffer base * @size Buffer size */ -void tap_sock_update_pool(void *base, size_t size) +static void tap_sock_update_pool(void *base, size_t size) { int i; @@ -1479,8 +1479,8 @@ void tap_sock_update_pool(void *base, size_t size) void tap_backend_init(struct ctx *c) { if (c->mode == MODE_VU) { - tap_sock_update_pool(NULL, 0); vu_init(c); + tap_sock_update_pool(&c->vdev->memory, 0); } else { tap_sock_update_pool(pkt_buf, sizeof(pkt_buf)); } diff --git a/tap.h b/tap.h index ce5510882d5d..21db4d219ecb 100644 --- a/tap.h +++ b/tap.h @@ -115,7 +115,6 @@ void tap_handler_passt(struct ctx *c, uint32_t events, const struct timespec *now); int tap_sock_unix_open(char *sock_path); void tap_sock_reset(struct ctx *c); -void tap_sock_update_pool(void *base, size_t size); void tap_backend_init(struct ctx *c); void tap_flush_pools(void); void tap_handler(struct ctx *c, const struct timespec *now); diff --git a/vhost_user.c b/vhost_user.c index c1522d549f00..f97ec6064cac 100644 --- a/vhost_user.c +++ b/vhost_user.c @@ -137,8 +137,8 @@ static void *qva_to_va(struct vu_dev *dev, uint64_t qemu_addr) unsigned int i; /* Find matching memory region. */ - for (i = 0; i < dev->nregions; i++) { - const struct vu_dev_region *r = &dev->regions[i]; + for (i = 0; i < dev->memory.nregions; i++) { + const struct vu_dev_region *r = &dev->memory.regions[i]; if ((qemu_addr >= r->qva) && (qemu_addr < (r->qva + r->size))) { /* NOLINTNEXTLINE(performance-no-int-to-ptr) */ @@ -428,8 +428,8 @@ static bool vu_set_mem_table_exec(struct vu_dev *vdev, struct vhost_user_memory m = vmsg->payload.memory, *memory = &m; unsigned int i; - for (i = 0; i < vdev->nregions; i++) { - const struct vu_dev_region *r = &vdev->regions[i]; + for (i = 0; i < vdev->memory.nregions; i++) { + const struct vu_dev_region *r = &vdev->memory.regions[i]; if (r->mmap_addr) { /* NOLINTNEXTLINE(performance-no-int-to-ptr) */ @@ -437,12 +437,12 @@ static bool vu_set_mem_table_exec(struct vu_dev *vdev, r->size + r->mmap_offset); } } - vdev->nregions = memory->nregions; + vdev->memory.nregions = memory->nregions; debug("vhost-user nregions: %u", memory->nregions); - for (i = 0; i < vdev->nregions; i++) { + for (i = 0; i < vdev->memory.nregions; i++) { struct vhost_user_memory_region *msg_region = &memory->regions[i]; - struct vu_dev_region *dev_region = &vdev->regions[i]; + struct vu_dev_region *dev_region = &vdev->memory.regions[i]; void *mmap_addr; debug("vhost-user region %d", i); @@ -484,13 +484,7 @@ static bool vu_set_mem_table_exec(struct vu_dev *vdev, } } - /* As vu_packet_check_range() has no access to the number of - * memory regions, mark the end of the array with mmap_addr = 0 - */ - ASSERT(vdev->nregions < VHOST_USER_MAX_RAM_SLOTS - 1); - vdev->regions[vdev->nregions].mmap_addr = 0; - - tap_sock_update_pool(vdev->regions, 0); + ASSERT(vdev->memory.nregions < VHOST_USER_MAX_RAM_SLOTS); return false; } @@ -1106,8 +1100,8 @@ void vu_cleanup(struct vu_dev *vdev) vq->vring.avail = 0; } - for (i = 0; i < vdev->nregions; i++) { - const struct vu_dev_region *r = &vdev->regions[i]; + for (i = 0; i < vdev->memory.nregions; i++) { + const struct vu_dev_region *r = &vdev->memory.regions[i]; if (r->mmap_addr) { /* NOLINTNEXTLINE(performance-no-int-to-ptr) */ @@ -1115,7 +1109,7 @@ void vu_cleanup(struct vu_dev *vdev) r->size + r->mmap_offset); } } - vdev->nregions = 0; + vdev->memory.nregions = 0; vu_close_log(vdev); diff --git a/virtio.c b/virtio.c index ed7842b4c78a..bd388c2dfc7f 100644 --- a/virtio.c +++ b/virtio.c @@ -102,8 +102,8 @@ static void *vu_gpa_to_va(const struct vu_dev *dev, uint64_t *plen, return NULL; /* Find matching memory region. */ - for (i = 0; i < dev->nregions; i++) { - const struct vu_dev_region *r = &dev->regions[i]; + for (i = 0; i < dev->memory.nregions; i++) { + const struct vu_dev_region *r = &dev->memory.regions[i]; if ((guest_addr >= r->gpa) && (guest_addr < (r->gpa + r->size))) { diff --git a/virtio.h b/virtio.h index 32757458ea95..b55cc4042521 100644 --- a/virtio.h +++ b/virtio.h @@ -96,11 +96,22 @@ struct vu_dev_region { */ #define VHOST_USER_MAX_RAM_SLOTS 32 +/** + * struct vdev_memory - Describes the shared memory regions for a vhost-user + * device + * @nregions: Number of shared memory regions + * @regions: Guest shared memory regions + */ +struct vdev_memory { + uint32_t nregions; + struct vu_dev_region regions[VHOST_USER_MAX_RAM_SLOTS]; +}; + /** * struct vu_dev - vhost-user device information * @context: Execution context - * @nregions: Number of shared memory regions - * @regions: Guest shared memory regions + * @memory: Shared memory regions + * @vq: Virtqueues of the device * @features: Vhost-user features * @protocol_features: Vhost-user protocol features * @log_call_fd: Eventfd to report logging update @@ -109,8 +120,7 @@ struct vu_dev_region { */ struct vu_dev { struct ctx *context; - uint32_t nregions; - struct vu_dev_region regions[VHOST_USER_MAX_RAM_SLOTS]; + struct vdev_memory memory; struct vu_virtq vq[VHOST_USER_MAX_QUEUES]; uint64_t features; uint64_t protocol_features; diff --git a/vu_common.c b/vu_common.c index b77b21420c57..b716070ea3c3 100644 --- a/vu_common.c +++ b/vu_common.c @@ -25,26 +25,28 @@ /** * vu_packet_check_range() - Check if a given memory zone is contained in * a mapped guest memory region - * @buf: Array of the available memory regions + * @memory: Array of the available memory regions * @ptr: Start of desired data range - * @size: Length of desired data range + * @len: Length of desired data range * * Return: 0 if the zone is in a mapped memory region, -1 otherwise */ -int vu_packet_check_range(void *buf, const char *ptr, size_t len) +int vu_packet_check_range(struct vdev_memory *memory, + const char *ptr, size_t len) { - struct vu_dev_region *dev_region; + struct vu_dev_region *dev_region = memory->regions; + unsigned int i; - for (dev_region = buf; dev_region->mmap_addr; dev_region++) { - uintptr_t base_addr = dev_region->mmap_addr + - dev_region->mmap_offset; + for (i = 0; i < memory->nregions; i++) { + uintptr_t base_addr = dev_region[i].mmap_addr + + dev_region[i].mmap_offset; /* NOLINTNEXTLINE(performance-no-int-to-ptr) */ const char *base = (const char *)base_addr; - ASSERT(base_addr >= dev_region->mmap_addr); + ASSERT(base_addr >= dev_region[i].mmap_addr); - if (len <= dev_region->size && base <= ptr && - (size_t)(ptr - base) <= dev_region->size - len) + if (len <= dev_region[i].size && base <= ptr && + (size_t)(ptr - base) <= dev_region[i].size - len) return 0; } -- 2.49.0