From nobody Thu Dec 18 00:06:31 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 517EA328269 for ; Wed, 17 Dec 2025 11:24:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765970673; cv=none; b=pEXc0mPeignVtttUs5YQAoshmeTKLUZc84sA5V7SDfXIHIJd06DiBUy6QAT3NQrPODmQ7UBVoNQVlKRMNgJPFlgMwkmMr0YovCzo03fyPRd3dlacnQTK1zDWeyoOruUfhJmBEwMcVduwsyaPko7LVeheP3MBn1VloaPvtzvu+Pw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765970673; c=relaxed/simple; bh=5ECYxaYeBJVIOWeqy/iDdDa/H00cLiMfLM4EhoJhPCA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=bdLxozaHHQIvkdNrf1PzZ0dpl1s0GurzHu33AroYW1CL7PSdqNgS4xHc1+xl3rSxcdR60///nboEFV4hFARJpM5u5bmDiWf8ZfWVVfPros1fob+CbpUrAfBslBgdiCZppr2attmd1L0kxBkndGw8BnUu4mMVJmec8e+ke+55o/w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=fail smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=be4qoz1q; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="be4qoz1q" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1765970669; 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=o5hO8Psqd6yn9uKa/WLE0Q8XbzNZAyPQD8caCcDa5fs=; b=be4qoz1qmu04nmPjA3sDYcwOcUg93dU1xyDGYiXg/9Ndgcx8PwVMtQvl1lj2Snh3/wzYis epV+gxV0RmjoLUSsRdQnKwbsgXIqla4TdgYSq/h2H4yBNmjnFh0HZHq+8ZGMlkKnmePpbo p6dFqrzmfFofWsSrSdYbxhBCWQk5Y88= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-691-M-yN3yq3O6eWjd8qp_mwMw-1; Wed, 17 Dec 2025 06:24:26 -0500 X-MC-Unique: M-yN3yq3O6eWjd8qp_mwMw-1 X-Mimecast-MFC-AGG-ID: M-yN3yq3O6eWjd8qp_mwMw_1765970665 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (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-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id F32F0180028B; Wed, 17 Dec 2025 11:24:24 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.224.69]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 781D5180045B; Wed, 17 Dec 2025 11:24:21 +0000 (UTC) From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= To: "Michael S . Tsirkin " Cc: Maxime Coquelin , Laurent Vivier , virtualization@lists.linux.dev, linux-kernel@vger.kernel.org, Stefano Garzarella , Yongji Xie , jasowang@redhat.com, Xuan Zhuo , Cindy Lu , =?UTF-8?q?Eugenio=20P=C3=A9rez?= Subject: [PATCH v10 1/8] vduse: add v1 API definition Date: Wed, 17 Dec 2025 12:24:07 +0100 Message-ID: <20251217112414.2374672-2-eperezma@redhat.com> In-Reply-To: <20251217112414.2374672-1-eperezma@redhat.com> References: <20251217112414.2374672-1-eperezma@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 This allows the kernel to detect whether the userspace VDUSE device supports the VQ group and ASID features. VDUSE devices that don't set the V1 API will not receive the new messages, and vdpa device will be created with only one vq group and asid. The next patches implement the new feature incrementally, only enabling the VDUSE device to set the V1 API version by the end of the series. Acked-by: Jason Wang Reviewed-by: Xie Yongji Signed-off-by: Eugenio P=C3=A9rez --- include/uapi/linux/vduse.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/include/uapi/linux/vduse.h b/include/uapi/linux/vduse.h index 10ad71aa00d6..ccb92a1efce0 100644 --- a/include/uapi/linux/vduse.h +++ b/include/uapi/linux/vduse.h @@ -10,6 +10,10 @@ =20 #define VDUSE_API_VERSION 0 =20 +/* VQ groups and ASID support */ + +#define VDUSE_API_VERSION_1 1 + /* * Get the version of VDUSE API that kernel supported (VDUSE_API_VERSION). * This is used for future extension. --=20 2.52.0 From nobody Thu Dec 18 00:06:31 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C243C340A43 for ; Wed, 17 Dec 2025 11:24:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765970676; cv=none; b=YTJ9UYmisGOcLfy+cfrMYKll2Bi0k61H79m+pDEIXauqmd0TQQSN0DT9HUhSUDyiH9lqjgXqFIOlfUVTO8QgpAoWIGrsE2L4ZDwxnA+MD/x93X3soWnuR9xw5JeILTrY9tzayeqbqhtO3gYzEycQ/8/PwaHWwC9yswf+5/Rqz1E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765970676; c=relaxed/simple; bh=/LpnAmyEayLuAqOFJJr4mnbGj3rXDApjAO1oe0oDnns=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=nCAwBIaITdoLmGLkrA2+KHbiE1Oy6pAjZ3Aip9Zbf7FTSmWu9kXgNixXrwcn3661cN0avMPnxmPvbKHAgweT9UiMm8xDYVt0GeJy9jugVRqehQKqmEhSNIhw89Cf4wjUpIwhLaYC/xKgcpZ6XhWr+64ONwUgV/PhbEWZiKZF+Zo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=IxEHvoW0; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="IxEHvoW0" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1765970672; 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=pAW16ppDXa4TZaWE0gUX/BtHjqpxdUroKNBVmvAPZT8=; b=IxEHvoW0YLLTKrN2QXABmt6+jcHa7NN0Gp7osx4Zwzm/lcANGPHWx5X2wfwUSoCrQKaRou OJ1tdasPuyr+lpZxvgfM2IOf/DU/gMQH6QhQdd0WEhQcy97iE5NwEgy82Bgzb0ZPGlL0IS DSqxvrTnD9hW75usM75WaLP7wu07+ys= 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-191-AaJSwNw6NwmhOL_iHQmOIQ-1; Wed, 17 Dec 2025 06:24:30 -0500 X-MC-Unique: AaJSwNw6NwmhOL_iHQmOIQ-1 X-Mimecast-MFC-AGG-ID: AaJSwNw6NwmhOL_iHQmOIQ_1765970669 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (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 5F760195DE61; Wed, 17 Dec 2025 11:24:29 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.224.69]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 7B569180044F; Wed, 17 Dec 2025 11:24:25 +0000 (UTC) From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= To: "Michael S . Tsirkin " Cc: Maxime Coquelin , Laurent Vivier , virtualization@lists.linux.dev, linux-kernel@vger.kernel.org, Stefano Garzarella , Yongji Xie , jasowang@redhat.com, Xuan Zhuo , Cindy Lu , =?UTF-8?q?Eugenio=20P=C3=A9rez?= Subject: [PATCH v10 2/8] vduse: add vq group support Date: Wed, 17 Dec 2025 12:24:08 +0100 Message-ID: <20251217112414.2374672-3-eperezma@redhat.com> In-Reply-To: <20251217112414.2374672-1-eperezma@redhat.com> References: <20251217112414.2374672-1-eperezma@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 This allows separate the different virtqueues in groups that shares the same address space. Asking the VDUSE device for the groups of the vq at the beginning as they're needed for the DMA API. Allocating 3 vq groups as net is the device that need the most groups: * Dataplane (guest passthrough) * CVQ * Shadowed vrings. Future versions of the series can include dynamic allocation of the groups array so VDUSE can declare more groups. Acked-by: Jason Wang Reviewed-by: Xie Yongji Signed-off-by: Eugenio P=C3=A9rez --- v6: * s/sepparate/separate (MST). * s/dev->api_version < 1/dev->api_version < VDUSE_API_VERSION_1 v5: * Revert core vdpa changes (Jason). * Fix group =3D=3D ngroup case in checking VQ_SETUP argument (Jason). v4: * Revert the "invalid vq group" concept and assume 0 if not set (Jason). * Make config->ngroups =3D=3D 0 invalid (Jason). v3: * Make the default group an invalid group as long as VDUSE device does not set it to some valid u32 value. Modify the vdpa core to take that into account (Jason). * Create the VDUSE_DEV_MAX_GROUPS instead of using a magic number v2: * Now the vq group is in vduse_vq_config struct instead of issuing one VDUSE message per vq. v1: * Fix: Remove BIT_ULL(VIRTIO_S_*), as _S_ is already the bit (Maxime) RFC v3: * Increase VDUSE_MAX_VQ_GROUPS to 0xffff (Jason). It was set to a lower value to reduce memory consumption, but vqs are already limited to that value and userspace VDUSE is able to allocate that many vqs. * Remove the descs vq group capability as it will not be used and we can add it on top. * Do not ask for vq groups in number of vq groups < 2. * Move the valid vq groups range check to vduse_validate_config. RFC v2: * Cache group information in kernel, as we need to provide the vq map tokens properly. * Add descs vq group to optimize SVQ forwarding and support indirect descriptors out of the box. --- drivers/vdpa/vdpa_user/vduse_dev.c | 48 ++++++++++++++++++++++++++---- include/uapi/linux/vduse.h | 12 ++++++-- 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/drivers/vdpa/vdpa_user/vduse_dev.c b/drivers/vdpa/vdpa_user/vd= use_dev.c index ae357d014564..b012dc3557b9 100644 --- a/drivers/vdpa/vdpa_user/vduse_dev.c +++ b/drivers/vdpa/vdpa_user/vduse_dev.c @@ -39,6 +39,7 @@ #define DRV_LICENSE "GPL v2" =20 #define VDUSE_DEV_MAX (1U << MINORBITS) +#define VDUSE_DEV_MAX_GROUPS 0xffff #define VDUSE_MAX_BOUNCE_SIZE (1024 * 1024 * 1024) #define VDUSE_MIN_BOUNCE_SIZE (1024 * 1024) #define VDUSE_BOUNCE_SIZE (64 * 1024 * 1024) @@ -58,6 +59,7 @@ struct vduse_virtqueue { struct vdpa_vq_state state; bool ready; bool kicked; + u32 vq_group; spinlock_t kick_lock; spinlock_t irq_lock; struct eventfd_ctx *kickfd; @@ -114,6 +116,7 @@ struct vduse_dev { u8 status; u32 vq_num; u32 vq_align; + u32 ngroups; struct vduse_umem *umem; struct mutex mem_lock; unsigned int bounce_size; @@ -455,6 +458,7 @@ static void vduse_dev_reset(struct vduse_dev *dev) vq->driver_addr =3D 0; vq->device_addr =3D 0; vq->num =3D 0; + vq->vq_group =3D 0; memset(&vq->state, 0, sizeof(vq->state)); =20 spin_lock(&vq->kick_lock); @@ -592,6 +596,16 @@ static int vduse_vdpa_set_vq_state(struct vdpa_device = *vdpa, u16 idx, return 0; } =20 +static u32 vduse_get_vq_group(struct vdpa_device *vdpa, u16 idx) +{ + struct vduse_dev *dev =3D vdpa_to_vduse(vdpa); + + if (dev->api_version < VDUSE_API_VERSION_1) + return 0; + + return dev->vqs[idx]->vq_group; +} + static int vduse_vdpa_get_vq_state(struct vdpa_device *vdpa, u16 idx, struct vdpa_vq_state *state) { @@ -789,6 +803,7 @@ static const struct vdpa_config_ops vduse_vdpa_config_o= ps =3D { .set_vq_cb =3D vduse_vdpa_set_vq_cb, .set_vq_num =3D vduse_vdpa_set_vq_num, .get_vq_size =3D vduse_vdpa_get_vq_size, + .get_vq_group =3D vduse_get_vq_group, .set_vq_ready =3D vduse_vdpa_set_vq_ready, .get_vq_ready =3D vduse_vdpa_get_vq_ready, .set_vq_state =3D vduse_vdpa_set_vq_state, @@ -1252,12 +1267,24 @@ static long vduse_dev_ioctl(struct file *file, unsi= gned int cmd, if (config.index >=3D dev->vq_num) break; =20 - if (!is_mem_zero((const char *)config.reserved, - sizeof(config.reserved))) + if (dev->api_version < VDUSE_API_VERSION_1 && config.group) + break; + + if (dev->api_version >=3D VDUSE_API_VERSION_1) { + if (config.group >=3D dev->ngroups) + break; + if (dev->status & VIRTIO_CONFIG_S_DRIVER_OK) + break; + } + + if (config.reserved1 || + !is_mem_zero((const char *)config.reserved2, + sizeof(config.reserved2))) break; =20 index =3D array_index_nospec(config.index, dev->vq_num); dev->vqs[index]->num_max =3D config.max_size; + dev->vqs[index]->vq_group =3D config.group; ret =3D 0; break; } @@ -1737,12 +1764,20 @@ static bool features_is_valid(struct vduse_dev_conf= ig *config) return true; } =20 -static bool vduse_validate_config(struct vduse_dev_config *config) +static bool vduse_validate_config(struct vduse_dev_config *config, + u64 api_version) { if (!is_mem_zero((const char *)config->reserved, sizeof(config->reserved))) return false; =20 + if (api_version < VDUSE_API_VERSION_1 && config->ngroups) + return false; + + if (api_version >=3D VDUSE_API_VERSION_1 && + (!config->ngroups || config->ngroups > VDUSE_DEV_MAX_GROUPS)) + return false; + if (config->vq_align > PAGE_SIZE) return false; =20 @@ -1858,6 +1893,9 @@ static int vduse_create_dev(struct vduse_dev_config *= config, dev->device_features =3D config->features; dev->device_id =3D config->device_id; dev->vendor_id =3D config->vendor_id; + dev->ngroups =3D (dev->api_version < VDUSE_API_VERSION_1) + ? 1 + : config->ngroups; dev->name =3D kstrdup(config->name, GFP_KERNEL); if (!dev->name) goto err_str; @@ -1936,7 +1974,7 @@ static long vduse_ioctl(struct file *file, unsigned i= nt cmd, break; =20 ret =3D -EINVAL; - if (vduse_validate_config(&config) =3D=3D false) + if (!vduse_validate_config(&config, control->api_version)) break; =20 buf =3D vmemdup_user(argp + size, config.config_size); @@ -2017,7 +2055,7 @@ static int vduse_dev_init_vdpa(struct vduse_dev *dev,= const char *name) =20 vdev =3D vdpa_alloc_device(struct vduse_vdpa, vdpa, dev->dev, &vduse_vdpa_config_ops, &vduse_map_ops, - 1, 1, name, true); + dev->ngroups, 1, name, true); if (IS_ERR(vdev)) return PTR_ERR(vdev); =20 diff --git a/include/uapi/linux/vduse.h b/include/uapi/linux/vduse.h index ccb92a1efce0..a3d51cf6df3a 100644 --- a/include/uapi/linux/vduse.h +++ b/include/uapi/linux/vduse.h @@ -31,6 +31,7 @@ * @features: virtio features * @vq_num: the number of virtqueues * @vq_align: the allocation alignment of virtqueue's metadata + * @ngroups: number of vq groups that VDUSE device declares * @reserved: for future use, needs to be initialized to zero * @config_size: the size of the configuration space * @config: the buffer of the configuration space @@ -45,7 +46,8 @@ struct vduse_dev_config { __u64 features; __u32 vq_num; __u32 vq_align; - __u32 reserved[13]; + __u32 ngroups; /* if VDUSE_API_VERSION >=3D 1 */ + __u32 reserved[12]; __u32 config_size; __u8 config[]; }; @@ -122,14 +124,18 @@ struct vduse_config_data { * struct vduse_vq_config - basic configuration of a virtqueue * @index: virtqueue index * @max_size: the max size of virtqueue - * @reserved: for future use, needs to be initialized to zero + * @reserved1: for future use, needs to be initialized to zero + * @group: virtqueue group + * @reserved2: for future use, needs to be initialized to zero * * Structure used by VDUSE_VQ_SETUP ioctl to setup a virtqueue. */ struct vduse_vq_config { __u32 index; __u16 max_size; - __u16 reserved[13]; + __u16 reserved1; + __u32 group; + __u16 reserved2[10]; }; =20 /* --=20 2.52.0 From nobody Thu Dec 18 00:06:31 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 49AD0272805 for ; Wed, 17 Dec 2025 11:24:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765970685; cv=none; b=hc8GPbpZx7S4i5aJ5fMQWE8LMV+bybiav4MkJ2T5SbQbejot5FgejUiKbEJOFVKOpJJH7X4JoYlu2rraEl6R1HoDUDLTCTQZC+B01Pp1WaaYnBLyRFUpf1SbDTgRFB4boopinXq6cmkBrOiOIzfEyu7vRI9bmwcatZ0z2Uv/SqE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765970685; c=relaxed/simple; bh=eBD2OaLpu1Cyox4ZSf2yn/+W4jzg80BrSrDGpSPUcvA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=iTFmu7cc+METIi2dLhDfiKHptwel2/CWqRBpCbyWoVsCPM5nXwr0De3knngFLQ72waSNgytfuJOJ8Fcyhu7lk2By7TP/IaQeAOFPDjXQsswClBUB1YXQNoBJnT3AsSCBsLoYmI/9FLHEiILI6PDiSj4BNY5UZC+ywz7y/0gHoMM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=dsxT+YgF; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="dsxT+YgF" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1765970677; 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=nK6quPA5qSL4bGNS/eK+f49HT9I05odQ92Q1qY9IW2I=; b=dsxT+YgFuAywSLNw0Yc6i9wrsi8KuPXY6OlFqoB/mWEiDCVxNYI5c5oxRn0YxK9mDg4ImT lGXvU9fIZZx8dT/FvWJ45XjzXt1WTEV/ktt9JBTZdtPOiH2Emq/uv245d9/1ebBDRCz5Tw wpgHcWFSfZ2uoj/l2mwWju0r/uVrBIA= Received: from mx-prod-mc-03.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-626-w-0qKNefOfG9lqHJAVozwA-1; Wed, 17 Dec 2025 06:24:34 -0500 X-MC-Unique: w-0qKNefOfG9lqHJAVozwA-1 X-Mimecast-MFC-AGG-ID: w-0qKNefOfG9lqHJAVozwA_1765970673 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (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-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 528D6195605B; Wed, 17 Dec 2025 11:24:33 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.224.69]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id DBDF1180044F; Wed, 17 Dec 2025 11:24:29 +0000 (UTC) From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= To: "Michael S . Tsirkin " Cc: Maxime Coquelin , Laurent Vivier , virtualization@lists.linux.dev, linux-kernel@vger.kernel.org, Stefano Garzarella , Yongji Xie , jasowang@redhat.com, Xuan Zhuo , Cindy Lu , =?UTF-8?q?Eugenio=20P=C3=A9rez?= Subject: [PATCH v10 3/8] vduse: return internal vq group struct as map token Date: Wed, 17 Dec 2025 12:24:09 +0100 Message-ID: <20251217112414.2374672-4-eperezma@redhat.com> In-Reply-To: <20251217112414.2374672-1-eperezma@redhat.com> References: <20251217112414.2374672-1-eperezma@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Return the internal struct that represents the vq group as virtqueue map token, instead of the device. This allows the map functions to access the information per group. At this moment all the virtqueues share the same vq group, that only can point to ASID 0. This change prepares the infrastructure for actual per-group address space handling Acked-by: Jason Wang Signed-off-by: Eugenio P=C3=A9rez --- v4: * Revert the "invalid vq group" concept, and assume 0 by default. * Revert unnecesary blank line addition (Jason) v3: * Adapt all virtio_map_ops callbacks to handle empty tokens in case of invalid groups. * Make setting status DRIVER_OK fail if vq group is not valid. * Remove the _int name suffix from struct vduse_vq_group. RFC v3: * Make the vq groups a dynamic array to support an arbitrary number of them. --- drivers/vdpa/vdpa_user/vduse_dev.c | 100 ++++++++++++++++++++++++++--- include/linux/virtio.h | 6 +- 2 files changed, 94 insertions(+), 12 deletions(-) diff --git a/drivers/vdpa/vdpa_user/vduse_dev.c b/drivers/vdpa/vdpa_user/vd= use_dev.c index b012dc3557b9..d64bcd9ce76e 100644 --- a/drivers/vdpa/vdpa_user/vduse_dev.c +++ b/drivers/vdpa/vdpa_user/vduse_dev.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -85,6 +86,10 @@ struct vduse_umem { struct mm_struct *mm; }; =20 +struct vduse_vq_group { + struct vduse_dev *dev; +}; + struct vduse_dev { struct vduse_vdpa *vdev; struct device *dev; @@ -118,6 +123,7 @@ struct vduse_dev { u32 vq_align; u32 ngroups; struct vduse_umem *umem; + struct vduse_vq_group *groups; struct mutex mem_lock; unsigned int bounce_size; struct mutex domain_lock; @@ -606,6 +612,17 @@ static u32 vduse_get_vq_group(struct vdpa_device *vdpa= , u16 idx) return dev->vqs[idx]->vq_group; } =20 +static union virtio_map vduse_get_vq_map(struct vdpa_device *vdpa, u16 idx) +{ + struct vduse_dev *dev =3D vdpa_to_vduse(vdpa); + u32 vq_group =3D vduse_get_vq_group(vdpa, idx); + union virtio_map ret =3D { + .group =3D &dev->groups[vq_group], + }; + + return ret; +} + static int vduse_vdpa_get_vq_state(struct vdpa_device *vdpa, u16 idx, struct vdpa_vq_state *state) { @@ -826,6 +843,7 @@ static const struct vdpa_config_ops vduse_vdpa_config_o= ps =3D { .get_vq_affinity =3D vduse_vdpa_get_vq_affinity, .reset =3D vduse_vdpa_reset, .set_map =3D vduse_vdpa_set_map, + .get_vq_map =3D vduse_get_vq_map, .free =3D vduse_vdpa_free, }; =20 @@ -833,7 +851,14 @@ static void vduse_dev_sync_single_for_device(union vir= tio_map token, dma_addr_t dma_addr, size_t size, enum dma_data_direction dir) { - struct vduse_iova_domain *domain =3D token.iova_domain; + struct vduse_dev *vdev; + struct vduse_iova_domain *domain; + + if (!token.group) + return; + + vdev =3D token.group->dev; + domain =3D vdev->domain; =20 vduse_domain_sync_single_for_device(domain, dma_addr, size, dir); } @@ -842,7 +867,14 @@ static void vduse_dev_sync_single_for_cpu(union virtio= _map token, dma_addr_t dma_addr, size_t size, enum dma_data_direction dir) { - struct vduse_iova_domain *domain =3D token.iova_domain; + struct vduse_dev *vdev; + struct vduse_iova_domain *domain; + + if (!token.group) + return; + + vdev =3D token.group->dev; + domain =3D vdev->domain; =20 vduse_domain_sync_single_for_cpu(domain, dma_addr, size, dir); } @@ -852,7 +884,14 @@ static dma_addr_t vduse_dev_map_page(union virtio_map = token, struct page *page, enum dma_data_direction dir, unsigned long attrs) { - struct vduse_iova_domain *domain =3D token.iova_domain; + struct vduse_dev *vdev; + struct vduse_iova_domain *domain; + + if (!token.group) + return DMA_MAPPING_ERROR; + + vdev =3D token.group->dev; + domain =3D vdev->domain; =20 return vduse_domain_map_page(domain, page, offset, size, dir, attrs); } @@ -861,7 +900,14 @@ static void vduse_dev_unmap_page(union virtio_map toke= n, dma_addr_t dma_addr, size_t size, enum dma_data_direction dir, unsigned long attrs) { - struct vduse_iova_domain *domain =3D token.iova_domain; + struct vduse_dev *vdev; + struct vduse_iova_domain *domain; + + if (!token.group) + return; + + vdev =3D token.group->dev; + domain =3D vdev->domain; =20 return vduse_domain_unmap_page(domain, dma_addr, size, dir, attrs); } @@ -869,11 +915,17 @@ static void vduse_dev_unmap_page(union virtio_map tok= en, dma_addr_t dma_addr, static void *vduse_dev_alloc_coherent(union virtio_map token, size_t size, dma_addr_t *dma_addr, gfp_t flag) { - struct vduse_iova_domain *domain =3D token.iova_domain; + struct vduse_dev *vdev; + struct vduse_iova_domain *domain; unsigned long iova; void *addr; =20 *dma_addr =3D DMA_MAPPING_ERROR; + if (!token.group) + return NULL; + + vdev =3D token.group->dev; + domain =3D vdev->domain; addr =3D vduse_domain_alloc_coherent(domain, size, (dma_addr_t *)&iova, flag); if (!addr) @@ -888,14 +940,28 @@ static void vduse_dev_free_coherent(union virtio_map = token, size_t size, void *vaddr, dma_addr_t dma_addr, unsigned long attrs) { - struct vduse_iova_domain *domain =3D token.iova_domain; + struct vduse_dev *vdev; + struct vduse_iova_domain *domain; + + if (!token.group) + return; + + vdev =3D token.group->dev; + domain =3D vdev->domain; =20 vduse_domain_free_coherent(domain, size, vaddr, dma_addr, attrs); } =20 static bool vduse_dev_need_sync(union virtio_map token, dma_addr_t dma_add= r) { - struct vduse_iova_domain *domain =3D token.iova_domain; + struct vduse_dev *vdev; + struct vduse_iova_domain *domain; + + if (!token.group) + return false; + + vdev =3D token.group->dev; + domain =3D vdev->domain; =20 return dma_addr < domain->bounce_size; } @@ -909,7 +975,14 @@ static int vduse_dev_mapping_error(union virtio_map to= ken, dma_addr_t dma_addr) =20 static size_t vduse_dev_max_mapping_size(union virtio_map token) { - struct vduse_iova_domain *domain =3D token.iova_domain; + struct vduse_dev *vdev; + struct vduse_iova_domain *domain; + + if (!token.group) + return 0; + + vdev =3D token.group->dev; + domain =3D vdev->domain; =20 return domain->bounce_size; } @@ -1727,6 +1800,7 @@ static int vduse_destroy_dev(char *name) if (dev->domain) vduse_domain_destroy(dev->domain); kfree(dev->name); + kfree(dev->groups); vduse_dev_destroy(dev); module_put(THIS_MODULE); =20 @@ -1896,6 +1970,13 @@ static int vduse_create_dev(struct vduse_dev_config = *config, dev->ngroups =3D (dev->api_version < VDUSE_API_VERSION_1) ? 1 : config->ngroups; + dev->groups =3D kcalloc(dev->ngroups, sizeof(dev->groups[0]), + GFP_KERNEL); + if (!dev->groups) + goto err_vq_groups; + for (u32 i =3D 0; i < dev->ngroups; ++i) + dev->groups[i].dev =3D dev; + dev->name =3D kstrdup(config->name, GFP_KERNEL); if (!dev->name) goto err_str; @@ -1932,6 +2013,8 @@ static int vduse_create_dev(struct vduse_dev_config *= config, err_idr: kfree(dev->name); err_str: + kfree(dev->groups); +err_vq_groups: vduse_dev_destroy(dev); err: return ret; @@ -2093,7 +2176,6 @@ static int vdpa_dev_add(struct vdpa_mgmt_dev *mdev, c= onst char *name, return -ENOMEM; } =20 - dev->vdev->vdpa.vmap.iova_domain =3D dev->domain; ret =3D _vdpa_register_device(&dev->vdev->vdpa, dev->vq_num); if (ret) { put_device(&dev->vdev->vdpa.dev); diff --git a/include/linux/virtio.h b/include/linux/virtio.h index 3626eb694728..818363eb1b1d 100644 --- a/include/linux/virtio.h +++ b/include/linux/virtio.h @@ -43,13 +43,13 @@ struct virtqueue { void *priv; }; =20 -struct vduse_iova_domain; +struct vduse_vq_group; =20 union virtio_map { /* Device that performs DMA */ struct device *dma_dev; - /* VDUSE specific mapping data */ - struct vduse_iova_domain *iova_domain; + /* VDUSE specific virtqueue group for doing map */ + struct vduse_vq_group *group; }; =20 int virtqueue_add_outbuf(struct virtqueue *vq, --=20 2.52.0 From nobody Thu Dec 18 00:06:31 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 617F634252D for ; Wed, 17 Dec 2025 11:24:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765970687; cv=none; b=mdVcBh0JMu8TtvURx1g6kGEasaCcqIct1Pi3R5mPfFl9FQvAev0cIOg2gP9Qpf1iMocNkZ9Wa3ogVj6yfaiO9RaQgCZl8qddkVF91oBB75qPbfOsQFi60mjOqpb94QYNMoVcwRWW8BDudQYdShIGuVnVkLDiLN2Ks/QeyX9fHII= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765970687; c=relaxed/simple; bh=X1M6J9+dIKSIzDc+fVLR0WvDyVHOGc8KzXnSDOE50Uk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Y5EF05w8jQM+bC6n9IqVp3BQK/D12PZTIJ5hNFIDRPLw8qeYtFW7oDgxLJ1jqXxzZYky0UthRhtzNj7QodJtMxO9vcsZMMX/jBD6CS4Kx/GQOnxxjLJHTR38B80wMsAtOdKR2W3Rj0k2GFDXg4NJDN8sISJ8/hB3qxEQwQ3hb8U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=NnfAp98b; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="NnfAp98b" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1765970682; 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=ZjuJlhdDNznWR9Xj3wMK1yvwzZv5UKds5cBizS2xYxg=; b=NnfAp98bLXV4pJayvgpUcV3nfLoh2KnsKugWxhryao2dlOQq9qhu0bLDsqXp/HJATTTpwM aR/WCFnHpD80dFkyzqAPs3Nr8HCn9fJnpaYIvr1tXMGojMLSP8SKkwe1aNOlsoxZPoNSV0 qyEquYTDX64O/B8YfckzARY03gnk56U= Received: from mx-prod-mc-05.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-448-1jmfh5rDOj60WFXmR1qSEw-1; Wed, 17 Dec 2025 06:24:38 -0500 X-MC-Unique: 1jmfh5rDOj60WFXmR1qSEw-1 X-Mimecast-MFC-AGG-ID: 1jmfh5rDOj60WFXmR1qSEw_1765970677 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (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-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 770351956080; Wed, 17 Dec 2025 11:24:37 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.224.69]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 0AECF180044F; Wed, 17 Dec 2025 11:24:33 +0000 (UTC) From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= To: "Michael S . Tsirkin " Cc: Maxime Coquelin , Laurent Vivier , virtualization@lists.linux.dev, linux-kernel@vger.kernel.org, Stefano Garzarella , Yongji Xie , jasowang@redhat.com, Xuan Zhuo , Cindy Lu , =?UTF-8?q?Eugenio=20P=C3=A9rez?= Subject: [PATCH v10 4/8] vduse: refactor vdpa_dev_add for goto err handling Date: Wed, 17 Dec 2025 12:24:10 +0100 Message-ID: <20251217112414.2374672-5-eperezma@redhat.com> In-Reply-To: <20251217112414.2374672-1-eperezma@redhat.com> References: <20251217112414.2374672-1-eperezma@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Next patches introduce more error paths in this function. Refactor it so they can be accomodated through gotos. Acked-by: Jason Wang Reviewed-by: Xie Yongji Signed-off-by: Eugenio P=C3=A9rez --- v6: New in v6. --- drivers/vdpa/vdpa_user/vduse_dev.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/drivers/vdpa/vdpa_user/vduse_dev.c b/drivers/vdpa/vdpa_user/vd= use_dev.c index d64bcd9ce76e..0390e012c584 100644 --- a/drivers/vdpa/vdpa_user/vduse_dev.c +++ b/drivers/vdpa/vdpa_user/vduse_dev.c @@ -2172,21 +2172,27 @@ static int vdpa_dev_add(struct vdpa_mgmt_dev *mdev,= const char *name, dev->bounce_size); mutex_unlock(&dev->domain_lock); if (!dev->domain) { - put_device(&dev->vdev->vdpa.dev); - return -ENOMEM; + ret =3D -ENOMEM; + goto domain_err; } =20 ret =3D _vdpa_register_device(&dev->vdev->vdpa, dev->vq_num); if (ret) { - put_device(&dev->vdev->vdpa.dev); - mutex_lock(&dev->domain_lock); - vduse_domain_destroy(dev->domain); - dev->domain =3D NULL; - mutex_unlock(&dev->domain_lock); - return ret; + goto register_err; } =20 return 0; + +register_err: + mutex_lock(&dev->domain_lock); + vduse_domain_destroy(dev->domain); + dev->domain =3D NULL; + mutex_unlock(&dev->domain_lock); + +domain_err: + put_device(&dev->vdev->vdpa.dev); + + return ret; } =20 static void vdpa_dev_del(struct vdpa_mgmt_dev *mdev, struct vdpa_device *d= ev) --=20 2.52.0 From nobody Thu Dec 18 00:06:31 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7902C343D9B for ; Wed, 17 Dec 2025 11:24:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765970693; cv=none; b=uEAZZWMTRYv52V7BfqU/euS4ymFFe7LwuePTt+5X9H1k0G7xAf0xRCYX7z5wNfMh9kgaoiPgt/vGyY49hRtgtaUec6g+wqwlfrRWjqSEK+9w9YJNkS64TSv+7baYdf8/Ee4aKQQRmgLfQQFx4OR8Yv8XsmqUn60pd7M2ELc+vzU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765970693; c=relaxed/simple; bh=YEQB5+llCpl5ZkQh9uvBu6SGRjTwM2ib1GhNT+R6IGo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=GyICCkrmNJ6PJ/+PhhTZ+iHEoWFQ7BG0qGoX/VGgnTEw3mYKyX+h6pVybvHNhoY1iUjLirHyr8/2f08CwHQrSm6Cteno191g9/Ulf4uDlA+EcrF7hcooaX04XSYD3T0mhog8r3OXpqNTV6zNIwSNClWzuIoCgTthD2SY3d5Rxb8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=N47U7lYr; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="N47U7lYr" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1765970687; 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=snXgCpARZB+ibsLYXvxwMLKCCnj2IKWu1Tu56Pdl/nE=; b=N47U7lYrMrcDijz3jtvZC2qOJzP/Rb9r4MilAAUcMERLtjgGGW3P565Z+H9Der2BXZCuK1 yuUaVvyiNNkEBuR5TA0jP+wP+c/V6bRd5C6O3Y5GHJBYdZQ3kiKTk5aomFHw5niw/y+9b7 xSy6SDlSC5Y3PNjxkdcBljkwrAQi0nM= Received: from mx-prod-mc-03.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-142-YhxbbkgkMcGX1qw2FXhF5w-1; Wed, 17 Dec 2025 06:24:42 -0500 X-MC-Unique: YhxbbkgkMcGX1qw2FXhF5w-1 X-Mimecast-MFC-AGG-ID: YhxbbkgkMcGX1qw2FXhF5w_1765970681 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (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-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 9CF22195605B; Wed, 17 Dec 2025 11:24:41 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.224.69]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id F2C64180044F; Wed, 17 Dec 2025 11:24:37 +0000 (UTC) From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= To: "Michael S . Tsirkin " Cc: Maxime Coquelin , Laurent Vivier , virtualization@lists.linux.dev, linux-kernel@vger.kernel.org, Stefano Garzarella , Yongji Xie , jasowang@redhat.com, Xuan Zhuo , Cindy Lu , =?UTF-8?q?Eugenio=20P=C3=A9rez?= Subject: [PATCH v10 5/8] vduse: remove unused vaddr parameter of vduse_domain_free_coherent Date: Wed, 17 Dec 2025 12:24:11 +0100 Message-ID: <20251217112414.2374672-6-eperezma@redhat.com> In-Reply-To: <20251217112414.2374672-1-eperezma@redhat.com> References: <20251217112414.2374672-1-eperezma@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 We will modify the function in next patches so let's clean it first. Signed-off-by: Eugenio P=C3=A9rez --- drivers/vdpa/vdpa_user/iova_domain.c | 3 +-- drivers/vdpa/vdpa_user/iova_domain.h | 3 +-- drivers/vdpa/vdpa_user/vduse_dev.c | 2 +- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/drivers/vdpa/vdpa_user/iova_domain.c b/drivers/vdpa/vdpa_user/= iova_domain.c index 4352b5cf74f0..309cd5a039d1 100644 --- a/drivers/vdpa/vdpa_user/iova_domain.c +++ b/drivers/vdpa/vdpa_user/iova_domain.c @@ -528,8 +528,7 @@ void *vduse_domain_alloc_coherent(struct vduse_iova_dom= ain *domain, } =20 void vduse_domain_free_coherent(struct vduse_iova_domain *domain, size_t s= ize, - void *vaddr, dma_addr_t dma_addr, - unsigned long attrs) + dma_addr_t dma_addr, unsigned long attrs) { struct iova_domain *iovad =3D &domain->consistent_iovad; struct vhost_iotlb_map *map; diff --git a/drivers/vdpa/vdpa_user/iova_domain.h b/drivers/vdpa/vdpa_user/= iova_domain.h index 775cad5238f3..42090cd1a622 100644 --- a/drivers/vdpa/vdpa_user/iova_domain.h +++ b/drivers/vdpa/vdpa_user/iova_domain.h @@ -72,8 +72,7 @@ void *vduse_domain_alloc_coherent(struct vduse_iova_domai= n *domain, gfp_t flag); =20 void vduse_domain_free_coherent(struct vduse_iova_domain *domain, size_t s= ize, - void *vaddr, dma_addr_t dma_addr, - unsigned long attrs); + dma_addr_t dma_addr, unsigned long attrs); =20 void vduse_domain_reset_bounce_map(struct vduse_iova_domain *domain); =20 diff --git a/drivers/vdpa/vdpa_user/vduse_dev.c b/drivers/vdpa/vdpa_user/vd= use_dev.c index 0390e012c584..a278cff7a4fa 100644 --- a/drivers/vdpa/vdpa_user/vduse_dev.c +++ b/drivers/vdpa/vdpa_user/vduse_dev.c @@ -949,7 +949,7 @@ static void vduse_dev_free_coherent(union virtio_map to= ken, size_t size, vdev =3D token.group->dev; domain =3D vdev->domain; =20 - vduse_domain_free_coherent(domain, size, vaddr, dma_addr, attrs); + vduse_domain_free_coherent(domain, size, dma_addr, attrs); } =20 static bool vduse_dev_need_sync(union virtio_map token, dma_addr_t dma_add= r) --=20 2.52.0 From nobody Thu Dec 18 00:06:31 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9AA2D34106C for ; Wed, 17 Dec 2025 11:24:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765970693; cv=none; b=s0ydkP0MObj0zP8WmNgj/Pc/19fgmVYU4j7yChzMoGhprxw3ZJCMA7cP7R0AX29LyWc1KIF5+DOBQzGZgY/85peAADxSh8H34JmD5f/CK/sq9nJGjshQ7IMZZPwzkCUdQmHxaCf/G5jNK0fJStw2RWcwDIDQ+64T6ZdFSt8Ruw4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765970693; c=relaxed/simple; bh=LxzE1hkrXg+IA1DydmXFVRxIMeeQYdOVJFcT12jTN/o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=iAcSQpOFcSpNAEN4w2NjDPcHJ2s7dsfwP8/jxy9owbuHS8+5EOW2kkNbMSv4O08cssMYxh7oh7Gb76PxvuYwJxZ58VQnNY/hNHuMc1DiqQM+dccDecdkIyny72dQGXeECe2DffY11WFfBGaWJunmAJCT2ArodOSr+D6BSgXq/1Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Zdi5K7SL; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Zdi5K7SL" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1765970690; 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=0IUcXwE5mBGW85/ZKVoXrLqsALgfq4FyValS2fu7D4Q=; b=Zdi5K7SLFfsFWQ1CxS7ygI/wnAHOwyT/eX9uAntgBAeomtCi4xQpJzdOo4YwVJMVdpn1m+ 7YNiR6a8Pn4OnxzaQsdBP8bsUp4xi42s1D4G5vCIs0rh5gqa949wgF6ObKTRWlDIkFUosn qFecd9cln4ZxdycAWvRy2tIV/3sbLmE= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-397-zbSsSkQkOuGkUHMjxrJ0dg-1; Wed, 17 Dec 2025 06:24:46 -0500 X-MC-Unique: zbSsSkQkOuGkUHMjxrJ0dg-1 X-Mimecast-MFC-AGG-ID: zbSsSkQkOuGkUHMjxrJ0dg_1765970685 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (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-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 9008F18002DE; Wed, 17 Dec 2025 11:24:45 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.224.69]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 25CE9180045B; Wed, 17 Dec 2025 11:24:41 +0000 (UTC) From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= To: "Michael S . Tsirkin " Cc: Maxime Coquelin , Laurent Vivier , virtualization@lists.linux.dev, linux-kernel@vger.kernel.org, Stefano Garzarella , Yongji Xie , jasowang@redhat.com, Xuan Zhuo , Cindy Lu , =?UTF-8?q?Eugenio=20P=C3=A9rez?= Subject: [PATCH v10 6/8] vduse: take out allocations from vduse_dev_alloc_coherent Date: Wed, 17 Dec 2025 12:24:12 +0100 Message-ID: <20251217112414.2374672-7-eperezma@redhat.com> In-Reply-To: <20251217112414.2374672-1-eperezma@redhat.com> References: <20251217112414.2374672-1-eperezma@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 The function vduse_dev_alloc_coherent will be called under rwlock in next patches. Make it out of the lock to avoid increasing its fail rate. Signed-off-by: Eugenio P=C3=A9rez --- drivers/vdpa/vdpa_user/iova_domain.c | 7 ++----- drivers/vdpa/vdpa_user/iova_domain.h | 2 +- drivers/vdpa/vdpa_user/vduse_dev.c | 13 +++++++++++-- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/drivers/vdpa/vdpa_user/iova_domain.c b/drivers/vdpa/vdpa_user/= iova_domain.c index 309cd5a039d1..0ae52890518c 100644 --- a/drivers/vdpa/vdpa_user/iova_domain.c +++ b/drivers/vdpa/vdpa_user/iova_domain.c @@ -495,14 +495,13 @@ void vduse_domain_unmap_page(struct vduse_iova_domain= *domain, =20 void *vduse_domain_alloc_coherent(struct vduse_iova_domain *domain, size_t size, dma_addr_t *dma_addr, - gfp_t flag) + void *orig) { struct iova_domain *iovad =3D &domain->consistent_iovad; unsigned long limit =3D domain->iova_limit; dma_addr_t iova =3D vduse_domain_alloc_iova(iovad, size, limit); - void *orig =3D alloc_pages_exact(size, flag); =20 - if (!iova || !orig) + if (!iova) goto err; =20 spin_lock(&domain->iotlb_lock); @@ -519,8 +518,6 @@ void *vduse_domain_alloc_coherent(struct vduse_iova_dom= ain *domain, return orig; err: *dma_addr =3D DMA_MAPPING_ERROR; - if (orig) - free_pages_exact(orig, size); if (iova) vduse_domain_free_iova(iovad, iova, size); =20 diff --git a/drivers/vdpa/vdpa_user/iova_domain.h b/drivers/vdpa/vdpa_user/= iova_domain.h index 42090cd1a622..86b7c7eadfd0 100644 --- a/drivers/vdpa/vdpa_user/iova_domain.h +++ b/drivers/vdpa/vdpa_user/iova_domain.h @@ -69,7 +69,7 @@ void vduse_domain_unmap_page(struct vduse_iova_domain *do= main, =20 void *vduse_domain_alloc_coherent(struct vduse_iova_domain *domain, size_t size, dma_addr_t *dma_addr, - gfp_t flag); + void *orig); =20 void vduse_domain_free_coherent(struct vduse_iova_domain *domain, size_t s= ize, dma_addr_t dma_addr, unsigned long attrs); diff --git a/drivers/vdpa/vdpa_user/vduse_dev.c b/drivers/vdpa/vdpa_user/vd= use_dev.c index a278cff7a4fa..767abcb7e375 100644 --- a/drivers/vdpa/vdpa_user/vduse_dev.c +++ b/drivers/vdpa/vdpa_user/vduse_dev.c @@ -924,16 +924,24 @@ static void *vduse_dev_alloc_coherent(union virtio_ma= p token, size_t size, if (!token.group) return NULL; =20 + addr =3D alloc_pages_exact(size, flag); + if (!addr) + return NULL; + vdev =3D token.group->dev; domain =3D vdev->domain; addr =3D vduse_domain_alloc_coherent(domain, size, - (dma_addr_t *)&iova, flag); + (dma_addr_t *)&iova, addr); if (!addr) - return NULL; + goto err; =20 *dma_addr =3D (dma_addr_t)iova; =20 return addr; + +err: + free_pages_exact(addr, size); + return NULL; } =20 static void vduse_dev_free_coherent(union virtio_map token, size_t size, @@ -950,6 +958,7 @@ static void vduse_dev_free_coherent(union virtio_map to= ken, size_t size, domain =3D vdev->domain; =20 vduse_domain_free_coherent(domain, size, dma_addr, attrs); + free_pages_exact(vaddr, size); } =20 static bool vduse_dev_need_sync(union virtio_map token, dma_addr_t dma_add= r) --=20 2.52.0 From nobody Thu Dec 18 00:06:31 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 78706345CB5 for ; Wed, 17 Dec 2025 11:24:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765970701; cv=none; b=uAxY8UysRbfyQ8HkVaeNZ2+pNAeNxD60dtAC8Uf6utvgx6BSqWsqYKMJm+MB/nSivGtjN9fEfPtlER2DQTiQAAdDykY5gT1TT+1abo6TaK2WAsJESwZIdgLu+bMTPmZ7gfhaApe8a9eoEnPKwo/V3HJ1g6E+gH8MqPdpzkJHAmU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765970701; c=relaxed/simple; bh=iBq1SCFq17la+Lne5riVPcxA0AFxFbe9ciwoSkeVB0g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=fbmaJ1p5XbHOz+dhx9hjMyMgJXqm0OJCmqkVr79xALu7ezIeDwWnABx3bv+avl4fUT1TFRz1w6exXNVRaQGI2b/RHC8SkKo6Ks+UroQiANz48iCepO+5aOszbb5E4GBKnu4boOW0e+asMjH7XILeYDt+U1AbUm9FUGsU2ro4n1M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=XyGep6oX; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="XyGep6oX" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1765970694; 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=8eEnAITPCevt7uNTl8qm1dNrFAtskIjea68uUNBmh+0=; b=XyGep6oXUYeYOrfBZ4/pWNDwKmRpzpdXn3MzpT4kfo0+ejXm4toBcdRmTl6fqynJftlENg Y4pKPW7HBgnm3nvYw241C8M3h6fbr4gCYMHhGso93hiF2PSigvxAazmSbojFMiQ4fKF9sy 927RO7L6NzH3bt79W8JY33KGvzhZgsU= 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-446-RBQz7jB5OZeFa2FkPuFqEg-1; Wed, 17 Dec 2025 06:24:51 -0500 X-MC-Unique: RBQz7jB5OZeFa2FkPuFqEg-1 X-Mimecast-MFC-AGG-ID: RBQz7jB5OZeFa2FkPuFqEg_1765970689 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (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 C62CE1956054; Wed, 17 Dec 2025 11:24:49 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.224.69]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 19E22180045B; Wed, 17 Dec 2025 11:24:45 +0000 (UTC) From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= To: "Michael S . Tsirkin " Cc: Maxime Coquelin , Laurent Vivier , virtualization@lists.linux.dev, linux-kernel@vger.kernel.org, Stefano Garzarella , Yongji Xie , jasowang@redhat.com, Xuan Zhuo , Cindy Lu , =?UTF-8?q?Eugenio=20P=C3=A9rez?= Subject: [PATCH v10 7/8] vduse: add vq group asid support Date: Wed, 17 Dec 2025 12:24:13 +0100 Message-ID: <20251217112414.2374672-8-eperezma@redhat.com> In-Reply-To: <20251217112414.2374672-1-eperezma@redhat.com> References: <20251217112414.2374672-1-eperezma@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Add support for assigning Address Space Identifiers (ASIDs) to each VQ group. This enables mapping each group into a distinct memory space. The vq group to ASID association is protected by a rwlock now. But the mutex domain_lock keeps protecting the domains of all ASIDs, as some operations like the one related with the bounce buffer size still requires to lock all the ASIDs. Signed-off-by: Eugenio P=C3=A9rez --- Future improvements can include performance optimizations on top could move to per-ASID locks, or hardening by tracking ASID or ASID hashes on unused bits of the DMA address. Tested virtio_vdpa by adding manually two threads in vduse_set_status: one of them modifies the vq group 0 ASID and the other one map and unmap memory continuously. After a while, the two threads stop and the usual work continues. Tested with vhost_vdpa by migrating a VM while ping on OVS+VDUSE. A few workaround were needed in some parts: * Do not enable CVQ before data vqs in QEMU, as VDUSE does not forward the enable message to the userland device. This will be solved in the future. * Share the suspended state between all vhost devices in QEMU: https://lists.nongnu.org/archive/html/qemu-devel/2025-11/msg02947.html * Implement a fake VDUSE suspend vdpa operation callback that always returns true in the kernel. DPDK suspend the device at the first GET_VRING_BASE. * Remove the CVQ blocker in ASID. --- v10: * Back to rwlock version so stronger locks are used. * Take out allocations from rwlock. * Forbid changing ASID of a vq group after DRIVER_OK (Jason) * Remove bad fetching again of domain variable in vduse_dev_max_mapping_size (Yongji). * Remove unused vdev definition in vdpa map_ops callbacks (kernel test robot). v9: * Replace mutex with rwlock, as the vdpa map_ops can run from atomic context. v8: * Revert the mutex to rwlock change, it needs proper profiling to justify it. v7: * Take write lock in the error path (Jason). v6: * Make vdpa_dev_add use gotos for error handling (MST). * s/(dev->api_version < 1) ?/(dev->api_version < VDUSE_API_VERSION_1) ?/ (MST). * Fix struct name not matching in the doc. v5: * Properly return errno if copy_to_user returns >0 in VDUSE_IOTLB_GET_FD ioctl (Jason). * Properly set domain bounce size to divide equally between nas (Jason). * Exclude "padding" member from the only >V1 members in vduse_dev_request. v4: * Divide each domain bounce size between the device bounce size (Jason). * revert unneeded addr =3D NULL assignment (Jason) * Change if (x && (y || z)) return to if (x) { if (y) return; if (z) return; } (Jason) * Change a bad multiline comment, using @ caracter instead of * (Jason). * Consider config->nas =3D=3D 0 as a fail (Jason). v3: * Get the vduse domain through the vduse_as in the map functions (Jason). * Squash with the patch creating the vduse_as struct (Jason). * Create VDUSE_DEV_MAX_AS instead of comparing agains a magic number (Jason) v2: * Convert the use of mutex to rwlock. RFC v3: * Increase VDUSE_MAX_VQ_GROUPS to 0xffff (Jason). It was set to a lower value to reduce memory consumption, but vqs are already limited to that value and userspace VDUSE is able to allocate that many vqs. * Remove TODO about merging VDUSE_IOTLB_GET_FD ioctl with VDUSE_IOTLB_GET_INFO. * Use of array_index_nospec in VDUSE device ioctls. * Embed vduse_iotlb_entry into vduse_iotlb_entry_v2. * Move the umem mutex to asid struct so there is no contention between ASIDs. RFC v2: * Make iotlb entry the last one of vduse_iotlb_entry_v2 so the first part of the struct is the same. --- drivers/vdpa/vdpa_user/vduse_dev.c | 366 +++++++++++++++++++---------- include/uapi/linux/vduse.h | 53 ++++- 2 files changed, 295 insertions(+), 124 deletions(-) diff --git a/drivers/vdpa/vdpa_user/vduse_dev.c b/drivers/vdpa/vdpa_user/vd= use_dev.c index 767abcb7e375..786ab2378825 100644 --- a/drivers/vdpa/vdpa_user/vduse_dev.c +++ b/drivers/vdpa/vdpa_user/vduse_dev.c @@ -41,6 +41,7 @@ =20 #define VDUSE_DEV_MAX (1U << MINORBITS) #define VDUSE_DEV_MAX_GROUPS 0xffff +#define VDUSE_DEV_MAX_AS 0xffff #define VDUSE_MAX_BOUNCE_SIZE (1024 * 1024 * 1024) #define VDUSE_MIN_BOUNCE_SIZE (1024 * 1024) #define VDUSE_BOUNCE_SIZE (64 * 1024 * 1024) @@ -86,7 +87,15 @@ struct vduse_umem { struct mm_struct *mm; }; =20 +struct vduse_as { + struct vduse_iova_domain *domain; + struct vduse_umem *umem; + struct mutex mem_lock; +}; + struct vduse_vq_group { + rwlock_t as_lock; + struct vduse_as *as; /* Protected by as_lock */ struct vduse_dev *dev; }; =20 @@ -94,7 +103,7 @@ struct vduse_dev { struct vduse_vdpa *vdev; struct device *dev; struct vduse_virtqueue **vqs; - struct vduse_iova_domain *domain; + struct vduse_as *as; char *name; struct mutex lock; spinlock_t msg_lock; @@ -122,9 +131,8 @@ struct vduse_dev { u32 vq_num; u32 vq_align; u32 ngroups; - struct vduse_umem *umem; + u32 nas; struct vduse_vq_group *groups; - struct mutex mem_lock; unsigned int bounce_size; struct mutex domain_lock; }; @@ -314,7 +322,7 @@ static int vduse_dev_set_status(struct vduse_dev *dev, = u8 status) return vduse_dev_msg_sync(dev, &msg); } =20 -static int vduse_dev_update_iotlb(struct vduse_dev *dev, +static int vduse_dev_update_iotlb(struct vduse_dev *dev, u32 asid, u64 start, u64 last) { struct vduse_dev_msg msg =3D { 0 }; @@ -323,8 +331,14 @@ static int vduse_dev_update_iotlb(struct vduse_dev *de= v, return -EINVAL; =20 msg.req.type =3D VDUSE_UPDATE_IOTLB; - msg.req.iova.start =3D start; - msg.req.iova.last =3D last; + if (dev->api_version < VDUSE_API_VERSION_1) { + msg.req.iova.start =3D start; + msg.req.iova.last =3D last; + } else { + msg.req.iova_v2.start =3D start; + msg.req.iova_v2.last =3D last; + msg.req.iova_v2.asid =3D asid; + } =20 return vduse_dev_msg_sync(dev, &msg); } @@ -436,14 +450,29 @@ static __poll_t vduse_dev_poll(struct file *file, pol= l_table *wait) return mask; } =20 +/* Force set the asid to a vq group without a message to the VDUSE device = */ +static void vduse_set_group_asid_nomsg(struct vduse_dev *dev, + unsigned int group, unsigned int asid) +{ + write_lock(&dev->groups[group].as_lock); + dev->groups[group].as =3D &dev->as[asid]; + write_unlock(&dev->groups[group].as_lock); +} + static void vduse_dev_reset(struct vduse_dev *dev) { int i; - struct vduse_iova_domain *domain =3D dev->domain; =20 /* The coherent mappings are handled in vduse_dev_free_coherent() */ - if (domain && domain->bounce_map) - vduse_domain_reset_bounce_map(domain); + for (i =3D 0; i < dev->nas; i++) { + struct vduse_iova_domain *domain =3D dev->as[i].domain; + + if (domain && domain->bounce_map) + vduse_domain_reset_bounce_map(domain); + } + + for (i =3D 0; i < dev->ngroups; i++) + vduse_set_group_asid_nomsg(dev, i, 0); =20 down_write(&dev->rwsem); =20 @@ -623,6 +652,30 @@ static union virtio_map vduse_get_vq_map(struct vdpa_d= evice *vdpa, u16 idx) return ret; } =20 +static int vduse_set_group_asid(struct vdpa_device *vdpa, unsigned int gro= up, + unsigned int asid) +{ + struct vduse_dev *dev =3D vdpa_to_vduse(vdpa); + struct vduse_dev_msg msg =3D { 0 }; + int r; + + if (dev->api_version < VDUSE_API_VERSION_1 || + group >=3D dev->ngroups || asid >=3D dev->nas || + dev->status & VIRTIO_CONFIG_S_DRIVER_OK) + return -EINVAL; + + msg.req.type =3D VDUSE_SET_VQ_GROUP_ASID; + msg.req.vq_group_asid.group =3D group; + msg.req.vq_group_asid.asid =3D asid; + + r =3D vduse_dev_msg_sync(dev, &msg); + if (r < 0) + return r; + + vduse_set_group_asid_nomsg(dev, group, asid); + return 0; +} + static int vduse_vdpa_get_vq_state(struct vdpa_device *vdpa, u16 idx, struct vdpa_vq_state *state) { @@ -794,13 +847,13 @@ static int vduse_vdpa_set_map(struct vdpa_device *vdp= a, struct vduse_dev *dev =3D vdpa_to_vduse(vdpa); int ret; =20 - ret =3D vduse_domain_set_map(dev->domain, iotlb); + ret =3D vduse_domain_set_map(dev->as[asid].domain, iotlb); if (ret) return ret; =20 - ret =3D vduse_dev_update_iotlb(dev, 0ULL, ULLONG_MAX); + ret =3D vduse_dev_update_iotlb(dev, asid, 0ULL, ULLONG_MAX); if (ret) { - vduse_domain_clear_map(dev->domain, iotlb); + vduse_domain_clear_map(dev->as[asid].domain, iotlb); return ret; } =20 @@ -843,6 +896,7 @@ static const struct vdpa_config_ops vduse_vdpa_config_o= ps =3D { .get_vq_affinity =3D vduse_vdpa_get_vq_affinity, .reset =3D vduse_vdpa_reset, .set_map =3D vduse_vdpa_set_map, + .set_group_asid =3D vduse_set_group_asid, .get_vq_map =3D vduse_get_vq_map, .free =3D vduse_vdpa_free, }; @@ -851,32 +905,30 @@ static void vduse_dev_sync_single_for_device(union vi= rtio_map token, dma_addr_t dma_addr, size_t size, enum dma_data_direction dir) { - struct vduse_dev *vdev; struct vduse_iova_domain *domain; =20 if (!token.group) return; =20 - vdev =3D token.group->dev; - domain =3D vdev->domain; - + read_lock(&token.group->as_lock); + domain =3D token.group->as->domain; vduse_domain_sync_single_for_device(domain, dma_addr, size, dir); + read_unlock(&token.group->as_lock); } =20 static void vduse_dev_sync_single_for_cpu(union virtio_map token, dma_addr_t dma_addr, size_t size, enum dma_data_direction dir) { - struct vduse_dev *vdev; struct vduse_iova_domain *domain; =20 if (!token.group) return; =20 - vdev =3D token.group->dev; - domain =3D vdev->domain; - + read_lock(&token.group->as_lock); + domain =3D token.group->as->domain; vduse_domain_sync_single_for_cpu(domain, dma_addr, size, dir); + read_unlock(&token.group->as_lock); } =20 static dma_addr_t vduse_dev_map_page(union virtio_map token, struct page *= page, @@ -884,38 +936,38 @@ static dma_addr_t vduse_dev_map_page(union virtio_map= token, struct page *page, enum dma_data_direction dir, unsigned long attrs) { - struct vduse_dev *vdev; struct vduse_iova_domain *domain; + dma_addr_t r; =20 if (!token.group) return DMA_MAPPING_ERROR; =20 - vdev =3D token.group->dev; - domain =3D vdev->domain; + read_lock(&token.group->as_lock); + domain =3D token.group->as->domain; + r =3D vduse_domain_map_page(domain, page, offset, size, dir, attrs); + read_unlock(&token.group->as_lock); =20 - return vduse_domain_map_page(domain, page, offset, size, dir, attrs); + return r; } =20 static void vduse_dev_unmap_page(union virtio_map token, dma_addr_t dma_ad= dr, size_t size, enum dma_data_direction dir, unsigned long attrs) { - struct vduse_dev *vdev; struct vduse_iova_domain *domain; =20 if (!token.group) return; =20 - vdev =3D token.group->dev; - domain =3D vdev->domain; - - return vduse_domain_unmap_page(domain, dma_addr, size, dir, attrs); + read_lock(&token.group->as_lock); + domain =3D token.group->as->domain; + vduse_domain_unmap_page(domain, dma_addr, size, dir, attrs); + read_unlock(&token.group->as_lock); } =20 static void *vduse_dev_alloc_coherent(union virtio_map token, size_t size, dma_addr_t *dma_addr, gfp_t flag) { - struct vduse_dev *vdev; struct vduse_iova_domain *domain; unsigned long iova; void *addr; @@ -928,18 +980,21 @@ static void *vduse_dev_alloc_coherent(union virtio_ma= p token, size_t size, if (!addr) return NULL; =20 - vdev =3D token.group->dev; - domain =3D vdev->domain; + *dma_addr =3D (dma_addr_t)iova; + read_lock(&token.group->as_lock); + domain =3D token.group->as->domain; addr =3D vduse_domain_alloc_coherent(domain, size, (dma_addr_t *)&iova, addr); if (!addr) goto err; =20 *dma_addr =3D (dma_addr_t)iova; + read_unlock(&token.group->as_lock); =20 return addr; =20 err: + read_unlock(&token.group->as_lock); free_pages_exact(addr, size); return NULL; } @@ -948,31 +1003,30 @@ static void vduse_dev_free_coherent(union virtio_map= token, size_t size, void *vaddr, dma_addr_t dma_addr, unsigned long attrs) { - struct vduse_dev *vdev; struct vduse_iova_domain *domain; =20 if (!token.group) return; =20 - vdev =3D token.group->dev; - domain =3D vdev->domain; - + read_lock(&token.group->as_lock); + domain =3D token.group->as->domain; vduse_domain_free_coherent(domain, size, dma_addr, attrs); + read_unlock(&token.group->as_lock); free_pages_exact(vaddr, size); } =20 static bool vduse_dev_need_sync(union virtio_map token, dma_addr_t dma_add= r) { - struct vduse_dev *vdev; - struct vduse_iova_domain *domain; + size_t bounce_size; =20 if (!token.group) return false; =20 - vdev =3D token.group->dev; - domain =3D vdev->domain; + read_lock(&token.group->as_lock); + bounce_size =3D token.group->as->domain->bounce_size; + read_unlock(&token.group->as_lock); =20 - return dma_addr < domain->bounce_size; + return dma_addr < bounce_size; } =20 static int vduse_dev_mapping_error(union virtio_map token, dma_addr_t dma_= addr) @@ -984,16 +1038,16 @@ static int vduse_dev_mapping_error(union virtio_map = token, dma_addr_t dma_addr) =20 static size_t vduse_dev_max_mapping_size(union virtio_map token) { - struct vduse_dev *vdev; - struct vduse_iova_domain *domain; + size_t bounce_size; =20 if (!token.group) return 0; =20 - vdev =3D token.group->dev; - domain =3D vdev->domain; + read_lock(&token.group->as_lock); + bounce_size =3D token.group->as->domain->bounce_size; + read_unlock(&token.group->as_lock); =20 - return domain->bounce_size; + return bounce_size; } =20 static const struct virtio_map_ops vduse_map_ops =3D { @@ -1133,39 +1187,40 @@ static int vduse_dev_queue_irq_work(struct vduse_de= v *dev, return ret; } =20 -static int vduse_dev_dereg_umem(struct vduse_dev *dev, +static int vduse_dev_dereg_umem(struct vduse_dev *dev, u32 asid, u64 iova, u64 size) { int ret; =20 - mutex_lock(&dev->mem_lock); + mutex_lock(&dev->as[asid].mem_lock); ret =3D -ENOENT; - if (!dev->umem) + if (!dev->as[asid].umem) goto unlock; =20 ret =3D -EINVAL; - if (!dev->domain) + if (!dev->as[asid].domain) goto unlock; =20 - if (dev->umem->iova !=3D iova || size !=3D dev->domain->bounce_size) + if (dev->as[asid].umem->iova !=3D iova || + size !=3D dev->as[asid].domain->bounce_size) goto unlock; =20 - vduse_domain_remove_user_bounce_pages(dev->domain); - unpin_user_pages_dirty_lock(dev->umem->pages, - dev->umem->npages, true); - atomic64_sub(dev->umem->npages, &dev->umem->mm->pinned_vm); - mmdrop(dev->umem->mm); - vfree(dev->umem->pages); - kfree(dev->umem); - dev->umem =3D NULL; + vduse_domain_remove_user_bounce_pages(dev->as[asid].domain); + unpin_user_pages_dirty_lock(dev->as[asid].umem->pages, + dev->as[asid].umem->npages, true); + atomic64_sub(dev->as[asid].umem->npages, &dev->as[asid].umem->mm->pinned_= vm); + mmdrop(dev->as[asid].umem->mm); + vfree(dev->as[asid].umem->pages); + kfree(dev->as[asid].umem); + dev->as[asid].umem =3D NULL; ret =3D 0; unlock: - mutex_unlock(&dev->mem_lock); + mutex_unlock(&dev->as[asid].mem_lock); return ret; } =20 static int vduse_dev_reg_umem(struct vduse_dev *dev, - u64 iova, u64 uaddr, u64 size) + u32 asid, u64 iova, u64 uaddr, u64 size) { struct page **page_list =3D NULL; struct vduse_umem *umem =3D NULL; @@ -1173,14 +1228,14 @@ static int vduse_dev_reg_umem(struct vduse_dev *dev, unsigned long npages, lock_limit; int ret; =20 - if (!dev->domain || !dev->domain->bounce_map || - size !=3D dev->domain->bounce_size || + if (!dev->as[asid].domain || !dev->as[asid].domain->bounce_map || + size !=3D dev->as[asid].domain->bounce_size || iova !=3D 0 || uaddr & ~PAGE_MASK) return -EINVAL; =20 - mutex_lock(&dev->mem_lock); + mutex_lock(&dev->as[asid].mem_lock); ret =3D -EEXIST; - if (dev->umem) + if (dev->as[asid].umem) goto unlock; =20 ret =3D -ENOMEM; @@ -1204,7 +1259,7 @@ static int vduse_dev_reg_umem(struct vduse_dev *dev, goto out; } =20 - ret =3D vduse_domain_add_user_bounce_pages(dev->domain, + ret =3D vduse_domain_add_user_bounce_pages(dev->as[asid].domain, page_list, pinned); if (ret) goto out; @@ -1217,7 +1272,7 @@ static int vduse_dev_reg_umem(struct vduse_dev *dev, umem->mm =3D current->mm; mmgrab(current->mm); =20 - dev->umem =3D umem; + dev->as[asid].umem =3D umem; out: if (ret && pinned > 0) unpin_user_pages(page_list, pinned); @@ -1228,7 +1283,7 @@ static int vduse_dev_reg_umem(struct vduse_dev *dev, vfree(page_list); kfree(umem); } - mutex_unlock(&dev->mem_lock); + mutex_unlock(&dev->as[asid].mem_lock); return ret; } =20 @@ -1260,47 +1315,66 @@ static long vduse_dev_ioctl(struct file *file, unsi= gned int cmd, =20 switch (cmd) { case VDUSE_IOTLB_GET_FD: { - struct vduse_iotlb_entry entry; + struct vduse_iotlb_entry_v2 entry; struct vhost_iotlb_map *map; struct vdpa_map_file *map_file; struct file *f =3D NULL; + u32 asid; =20 ret =3D -EFAULT; - if (copy_from_user(&entry, argp, sizeof(entry))) - break; + if (dev->api_version >=3D VDUSE_API_VERSION_1) { + if (copy_from_user(&entry, argp, sizeof(entry))) + break; + } else { + entry.asid =3D 0; + if (copy_from_user(&entry.v1, argp, + sizeof(entry.v1))) + break; + } =20 ret =3D -EINVAL; - if (entry.start > entry.last) + if (entry.v1.start > entry.v1.last) + break; + + if (entry.asid >=3D dev->nas) break; =20 mutex_lock(&dev->domain_lock); - if (!dev->domain) { + asid =3D array_index_nospec(entry.asid, dev->nas); + if (!dev->as[asid].domain) { mutex_unlock(&dev->domain_lock); break; } - spin_lock(&dev->domain->iotlb_lock); - map =3D vhost_iotlb_itree_first(dev->domain->iotlb, - entry.start, entry.last); + spin_lock(&dev->as[asid].domain->iotlb_lock); + map =3D vhost_iotlb_itree_first(dev->as[asid].domain->iotlb, + entry.v1.start, entry.v1.last); if (map) { map_file =3D (struct vdpa_map_file *)map->opaque; f =3D get_file(map_file->file); - entry.offset =3D map_file->offset; - entry.start =3D map->start; - entry.last =3D map->last; - entry.perm =3D map->perm; + entry.v1.offset =3D map_file->offset; + entry.v1.start =3D map->start; + entry.v1.last =3D map->last; + entry.v1.perm =3D map->perm; } - spin_unlock(&dev->domain->iotlb_lock); + spin_unlock(&dev->as[asid].domain->iotlb_lock); mutex_unlock(&dev->domain_lock); ret =3D -EINVAL; if (!f) break; =20 - ret =3D -EFAULT; - if (copy_to_user(argp, &entry, sizeof(entry))) { + if (dev->api_version >=3D VDUSE_API_VERSION_1) + ret =3D copy_to_user(argp, &entry, + sizeof(entry)); + else + ret =3D copy_to_user(argp, &entry.v1, + sizeof(entry.v1)); + + if (ret) { + ret =3D -EFAULT; fput(f); break; } - ret =3D receive_fd(f, NULL, perm_to_file_flags(entry.perm)); + ret =3D receive_fd(f, NULL, perm_to_file_flags(entry.v1.perm)); fput(f); break; } @@ -1445,6 +1519,7 @@ static long vduse_dev_ioctl(struct file *file, unsign= ed int cmd, } case VDUSE_IOTLB_REG_UMEM: { struct vduse_iova_umem umem; + u32 asid; =20 ret =3D -EFAULT; if (copy_from_user(&umem, argp, sizeof(umem))) @@ -1452,17 +1527,21 @@ static long vduse_dev_ioctl(struct file *file, unsi= gned int cmd, =20 ret =3D -EINVAL; if (!is_mem_zero((const char *)umem.reserved, - sizeof(umem.reserved))) + sizeof(umem.reserved)) || + (dev->api_version < VDUSE_API_VERSION_1 && + umem.asid !=3D 0) || umem.asid >=3D dev->nas) break; =20 mutex_lock(&dev->domain_lock); - ret =3D vduse_dev_reg_umem(dev, umem.iova, + asid =3D array_index_nospec(umem.asid, dev->nas); + ret =3D vduse_dev_reg_umem(dev, asid, umem.iova, umem.uaddr, umem.size); mutex_unlock(&dev->domain_lock); break; } case VDUSE_IOTLB_DEREG_UMEM: { struct vduse_iova_umem umem; + u32 asid; =20 ret =3D -EFAULT; if (copy_from_user(&umem, argp, sizeof(umem))) @@ -1470,10 +1549,15 @@ static long vduse_dev_ioctl(struct file *file, unsi= gned int cmd, =20 ret =3D -EINVAL; if (!is_mem_zero((const char *)umem.reserved, - sizeof(umem.reserved))) + sizeof(umem.reserved)) || + (dev->api_version < VDUSE_API_VERSION_1 && + umem.asid !=3D 0) || + umem.asid >=3D dev->nas) break; + mutex_lock(&dev->domain_lock); - ret =3D vduse_dev_dereg_umem(dev, umem.iova, + asid =3D array_index_nospec(umem.asid, dev->nas); + ret =3D vduse_dev_dereg_umem(dev, asid, umem.iova, umem.size); mutex_unlock(&dev->domain_lock); break; @@ -1481,6 +1565,7 @@ static long vduse_dev_ioctl(struct file *file, unsign= ed int cmd, case VDUSE_IOTLB_GET_INFO: { struct vduse_iova_info info; struct vhost_iotlb_map *map; + u32 asid; =20 ret =3D -EFAULT; if (copy_from_user(&info, argp, sizeof(info))) @@ -1494,23 +1579,31 @@ static long vduse_dev_ioctl(struct file *file, unsi= gned int cmd, sizeof(info.reserved))) break; =20 + if (dev->api_version < VDUSE_API_VERSION_1) { + if (info.asid) + break; + } else if (info.asid >=3D dev->nas) + break; + mutex_lock(&dev->domain_lock); - if (!dev->domain) { + asid =3D array_index_nospec(info.asid, dev->nas); + if (!dev->as[asid].domain) { mutex_unlock(&dev->domain_lock); break; } - spin_lock(&dev->domain->iotlb_lock); - map =3D vhost_iotlb_itree_first(dev->domain->iotlb, + spin_lock(&dev->as[asid].domain->iotlb_lock); + map =3D vhost_iotlb_itree_first(dev->as[asid].domain->iotlb, info.start, info.last); if (map) { info.start =3D map->start; info.last =3D map->last; info.capability =3D 0; - if (dev->domain->bounce_map && map->start =3D=3D 0 && - map->last =3D=3D dev->domain->bounce_size - 1) + if (dev->as[asid].domain->bounce_map && + map->start =3D=3D 0 && + map->last =3D=3D dev->as[asid].domain->bounce_size - 1) info.capability |=3D VDUSE_IOVA_CAP_UMEM; } - spin_unlock(&dev->domain->iotlb_lock); + spin_unlock(&dev->as[asid].domain->iotlb_lock); mutex_unlock(&dev->domain_lock); if (!map) break; @@ -1535,8 +1628,10 @@ static int vduse_dev_release(struct inode *inode, st= ruct file *file) struct vduse_dev *dev =3D file->private_data; =20 mutex_lock(&dev->domain_lock); - if (dev->domain) - vduse_dev_dereg_umem(dev, 0, dev->domain->bounce_size); + for (int i =3D 0; i < dev->nas; i++) + if (dev->as[i].domain) + vduse_dev_dereg_umem(dev, i, 0, + dev->as[i].domain->bounce_size); mutex_unlock(&dev->domain_lock); spin_lock(&dev->msg_lock); /* Make sure the inflight messages can processed after reconncection */ @@ -1755,7 +1850,6 @@ static struct vduse_dev *vduse_dev_create(void) return NULL; =20 mutex_init(&dev->lock); - mutex_init(&dev->mem_lock); mutex_init(&dev->domain_lock); spin_lock_init(&dev->msg_lock); INIT_LIST_HEAD(&dev->send_list); @@ -1806,8 +1900,11 @@ static int vduse_destroy_dev(char *name) idr_remove(&vduse_idr, dev->minor); kvfree(dev->config); vduse_dev_deinit_vqs(dev); - if (dev->domain) - vduse_domain_destroy(dev->domain); + for (int i =3D 0; i < dev->nas; i++) { + if (dev->as[i].domain) + vduse_domain_destroy(dev->as[i].domain); + } + kfree(dev->as); kfree(dev->name); kfree(dev->groups); vduse_dev_destroy(dev); @@ -1854,12 +1951,17 @@ static bool vduse_validate_config(struct vduse_dev_= config *config, sizeof(config->reserved))) return false; =20 - if (api_version < VDUSE_API_VERSION_1 && config->ngroups) + if (api_version < VDUSE_API_VERSION_1 && + (config->ngroups || config->nas)) return false; =20 - if (api_version >=3D VDUSE_API_VERSION_1 && - (!config->ngroups || config->ngroups > VDUSE_DEV_MAX_GROUPS)) - return false; + if (api_version >=3D VDUSE_API_VERSION_1) { + if (!config->ngroups || config->ngroups > VDUSE_DEV_MAX_GROUPS) + return false; + + if (!config->nas || config->nas > VDUSE_DEV_MAX_AS) + return false; + } =20 if (config->vq_align > PAGE_SIZE) return false; @@ -1924,7 +2026,8 @@ static ssize_t bounce_size_store(struct device *devic= e, =20 ret =3D -EPERM; mutex_lock(&dev->domain_lock); - if (dev->domain) + /* Assuming that if the first domain is allocated, all are allocated */ + if (dev->as[0].domain) goto unlock; =20 ret =3D kstrtouint(buf, 10, &bounce_size); @@ -1983,8 +2086,17 @@ static int vduse_create_dev(struct vduse_dev_config = *config, GFP_KERNEL); if (!dev->groups) goto err_vq_groups; - for (u32 i =3D 0; i < dev->ngroups; ++i) + for (u32 i =3D 0; i < dev->ngroups; ++i) { dev->groups[i].dev =3D dev; + rwlock_init(&dev->groups[i].as_lock); + } + + dev->nas =3D (dev->api_version < VDUSE_API_VERSION_1) ? 1 : config->nas; + dev->as =3D kcalloc(dev->nas, sizeof(dev->as[0]), GFP_KERNEL); + if (!dev->as) + goto err_as; + for (int i =3D 0; i < dev->nas; i++) + mutex_init(&dev->as[i].mem_lock); =20 dev->name =3D kstrdup(config->name, GFP_KERNEL); if (!dev->name) @@ -2022,6 +2134,8 @@ static int vduse_create_dev(struct vduse_dev_config *= config, err_idr: kfree(dev->name); err_str: + kfree(dev->as); +err_as: kfree(dev->groups); err_vq_groups: vduse_dev_destroy(dev); @@ -2147,7 +2261,7 @@ static int vduse_dev_init_vdpa(struct vduse_dev *dev,= const char *name) =20 vdev =3D vdpa_alloc_device(struct vduse_vdpa, vdpa, dev->dev, &vduse_vdpa_config_ops, &vduse_map_ops, - dev->ngroups, 1, name, true); + dev->ngroups, dev->nas, name, true); if (IS_ERR(vdev)) return PTR_ERR(vdev); =20 @@ -2162,7 +2276,8 @@ static int vdpa_dev_add(struct vdpa_mgmt_dev *mdev, c= onst char *name, const struct vdpa_dev_set_config *config) { struct vduse_dev *dev; - int ret; + size_t domain_bounce_size; + int ret, i; =20 mutex_lock(&vduse_lock); dev =3D vduse_find_dev(name); @@ -2176,29 +2291,38 @@ static int vdpa_dev_add(struct vdpa_mgmt_dev *mdev,= const char *name, return ret; =20 mutex_lock(&dev->domain_lock); - if (!dev->domain) - dev->domain =3D vduse_domain_create(VDUSE_IOVA_SIZE - 1, - dev->bounce_size); - mutex_unlock(&dev->domain_lock); - if (!dev->domain) { - ret =3D -ENOMEM; - goto domain_err; + ret =3D 0; + + domain_bounce_size =3D dev->bounce_size / dev->nas; + for (i =3D 0; i < dev->nas; ++i) { + dev->as[i].domain =3D vduse_domain_create(VDUSE_IOVA_SIZE - 1, + domain_bounce_size); + if (!dev->as[i].domain) { + ret =3D -ENOMEM; + goto err; + } } =20 + mutex_unlock(&dev->domain_lock); + ret =3D _vdpa_register_device(&dev->vdev->vdpa, dev->vq_num); - if (ret) { - goto register_err; - } + if (ret) + goto err_register; =20 return 0; =20 -register_err: +err_register: mutex_lock(&dev->domain_lock); - vduse_domain_destroy(dev->domain); - dev->domain =3D NULL; + +err: + for (int j =3D 0; j < i; j++) { + if (dev->as[j].domain) { + vduse_domain_destroy(dev->as[j].domain); + dev->as[j].domain =3D NULL; + } + } mutex_unlock(&dev->domain_lock); =20 -domain_err: put_device(&dev->vdev->vdpa.dev); =20 return ret; diff --git a/include/uapi/linux/vduse.h b/include/uapi/linux/vduse.h index a3d51cf6df3a..da2c5e47990e 100644 --- a/include/uapi/linux/vduse.h +++ b/include/uapi/linux/vduse.h @@ -47,7 +47,8 @@ struct vduse_dev_config { __u32 vq_num; __u32 vq_align; __u32 ngroups; /* if VDUSE_API_VERSION >=3D 1 */ - __u32 reserved[12]; + __u32 nas; /* if VDUSE_API_VERSION >=3D 1 */ + __u32 reserved[11]; __u32 config_size; __u8 config[]; }; @@ -82,6 +83,18 @@ struct vduse_iotlb_entry { __u8 perm; }; =20 +/** + * struct vduse_iotlb_entry_v2 - entry of IOTLB to describe one IOVA regio= n in an ASID + * @v1: the original vduse_iotlb_entry + * @asid: address space ID of the IOVA region + * + * Structure used by VDUSE_IOTLB_GET_FD ioctl to find an overlapped IOVA r= egion. + */ +struct vduse_iotlb_entry_v2 { + struct vduse_iotlb_entry v1; + __u32 asid; +}; + /* * Find the first IOVA region that overlaps with the range [start, last] * and return the corresponding file descriptor. Return -EINVAL means the @@ -166,6 +179,16 @@ struct vduse_vq_state_packed { __u16 last_used_idx; }; =20 +/** + * struct vduse_vq_group_asid - virtqueue group ASID + * @group: Index of the virtqueue group + * @asid: Address space ID of the group + */ +struct vduse_vq_group_asid { + __u32 group; + __u32 asid; +}; + /** * struct vduse_vq_info - information of a virtqueue * @index: virtqueue index @@ -225,6 +248,7 @@ struct vduse_vq_eventfd { * @uaddr: start address of userspace memory, it must be aligned to page s= ize * @iova: start of the IOVA region * @size: size of the IOVA region + * @asid: Address space ID of the IOVA region * @reserved: for future use, needs to be initialized to zero * * Structure used by VDUSE_IOTLB_REG_UMEM and VDUSE_IOTLB_DEREG_UMEM @@ -234,7 +258,8 @@ struct vduse_iova_umem { __u64 uaddr; __u64 iova; __u64 size; - __u64 reserved[3]; + __u32 asid; + __u32 reserved[5]; }; =20 /* Register userspace memory for IOVA regions */ @@ -248,6 +273,7 @@ struct vduse_iova_umem { * @start: start of the IOVA region * @last: last of the IOVA region * @capability: capability of the IOVA region + * @asid: Address space ID of the IOVA region, only if device API version = >=3D 1 * @reserved: for future use, needs to be initialized to zero * * Structure used by VDUSE_IOTLB_GET_INFO ioctl to get information of @@ -258,7 +284,8 @@ struct vduse_iova_info { __u64 last; #define VDUSE_IOVA_CAP_UMEM (1 << 0) __u64 capability; - __u64 reserved[3]; + __u32 asid; /* Only if device API version >=3D 1 */ + __u32 reserved[5]; }; =20 /* @@ -280,6 +307,7 @@ enum vduse_req_type { VDUSE_GET_VQ_STATE, VDUSE_SET_STATUS, VDUSE_UPDATE_IOTLB, + VDUSE_SET_VQ_GROUP_ASID, }; =20 /** @@ -314,6 +342,18 @@ struct vduse_iova_range { __u64 last; }; =20 +/** + * struct vduse_iova_range - IOVA range [start, last] if API_VERSION >=3D 1 + * @start: start of the IOVA range + * @last: last of the IOVA range + * @asid: address space ID of the IOVA range + */ +struct vduse_iova_range_v2 { + __u64 start; + __u64 last; + __u32 asid; +}; + /** * struct vduse_dev_request - control request * @type: request type @@ -322,6 +362,8 @@ struct vduse_iova_range { * @vq_state: virtqueue state, only index field is available * @s: device status * @iova: IOVA range for updating + * @iova_v2: IOVA range for updating if API_VERSION >=3D 1 + * @vq_group_asid: ASID of a virtqueue group * @padding: padding * * Structure used by read(2) on /dev/vduse/$NAME. @@ -334,6 +376,11 @@ struct vduse_dev_request { struct vduse_vq_state vq_state; struct vduse_dev_status s; struct vduse_iova_range iova; + /* Following members but padding exist only if vduse api + * version >=3D 1 + */; + struct vduse_iova_range_v2 iova_v2; + struct vduse_vq_group_asid vq_group_asid; __u32 padding[32]; }; }; --=20 2.52.0 From nobody Thu Dec 18 00:06:31 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 94CD73469F4 for ; Wed, 17 Dec 2025 11:25:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765970707; cv=none; b=tHsnqOWtU01IDLoL9ymOWTUtqTRJ0hKQdgT2EcXIeJfF0BpwESJOMeHtbGEYNlpKU4edF/KoKJbvohNRstU6OhdPzDCGUBMPV9nAFe2YHrJtEq92KEG4l4lbzULMTJquAFZuAFkoogqv58ZPPZ8lZq13oDpq3EdNmxdm+cwbr8I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765970707; c=relaxed/simple; bh=khCzcX8EHEIkWBj/FiSPOZVVK4O1q4782W4oYNETP30=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=eerxuh2ADCuKMZHxNFsmCws1p1qeH2m09FA68D5e0QUO7CERrMZYlXwT46ij1tyB7o2PgDuWWOBk5K3n3VqNf25SAuoXH6eEp5IPJnU6W6rg3s9ny9d3fu4/R1AtUDRCL0ls8zDK/wgxxyj+SGKj0ny70gg/2M7HD5gUZvrgOUY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=KFgFW+a2; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="KFgFW+a2" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1765970699; 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=IR90zvwtlGu1kNgK8+5PQ9pZR4Ny/J1mNtZRGeYkK4U=; b=KFgFW+a2r7f07I/F++IJSy1GXgV3W9ti0cb2hGjXWxd1P8851UaXB94Oq9jsGOWQRLggPu VC9CXQzLKW0a/5tUh9Zv0H+E1Aaryw2lf89xoFmvk8HEYPIh+JgjrPK1afyxwgGLeUIiAd q1vPuWtCQsWYfPmVTp0r6ipkhRZ8LAs= Received: from mx-prod-mc-05.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-529-M2Am3ypsMLSuVCFbSI31tA-1; Wed, 17 Dec 2025 06:24:55 -0500 X-MC-Unique: M2Am3ypsMLSuVCFbSI31tA-1 X-Mimecast-MFC-AGG-ID: M2Am3ypsMLSuVCFbSI31tA_1765970694 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (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-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 002601956094; Wed, 17 Dec 2025 11:24:54 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.224.69]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 7DD1C180045B; Wed, 17 Dec 2025 11:24:50 +0000 (UTC) From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= To: "Michael S . Tsirkin " Cc: Maxime Coquelin , Laurent Vivier , virtualization@lists.linux.dev, linux-kernel@vger.kernel.org, Stefano Garzarella , Yongji Xie , jasowang@redhat.com, Xuan Zhuo , Cindy Lu , =?UTF-8?q?Eugenio=20P=C3=A9rez?= Subject: [PATCH v10 8/8] vduse: bump version number Date: Wed, 17 Dec 2025 12:24:14 +0100 Message-ID: <20251217112414.2374672-9-eperezma@redhat.com> In-Reply-To: <20251217112414.2374672-1-eperezma@redhat.com> References: <20251217112414.2374672-1-eperezma@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Finalize the series by advertising VDUSE API v1 support to userspace. Now that all required infrastructure for v1 (ASIDs, VQ groups, update_iotlb_v2) is in place, VDUSE devices can opt in to the new features. Acked-by: Jason Wang Reviewed-by: Xie Yongji Signed-off-by: Eugenio P=C3=A9rez --- drivers/vdpa/vdpa_user/vduse_dev.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/vdpa/vdpa_user/vduse_dev.c b/drivers/vdpa/vdpa_user/vd= use_dev.c index 786ab2378825..15860bd331a6 100644 --- a/drivers/vdpa/vdpa_user/vduse_dev.c +++ b/drivers/vdpa/vdpa_user/vduse_dev.c @@ -2163,7 +2163,7 @@ static long vduse_ioctl(struct file *file, unsigned i= nt cmd, break; =20 ret =3D -EINVAL; - if (api_version > VDUSE_API_VERSION) + if (api_version > VDUSE_API_VERSION_1) break; =20 ret =3D 0; @@ -2230,7 +2230,7 @@ static int vduse_open(struct inode *inode, struct fil= e *file) if (!control) return -ENOMEM; =20 - control->api_version =3D VDUSE_API_VERSION; + control->api_version =3D VDUSE_API_VERSION_1; file->private_data =3D control; =20 return 0; --=20 2.52.0