From nobody Thu Oct 2 13:01:36 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 12103307AD7 for ; Tue, 16 Sep 2025 13:08: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=1758028133; cv=none; b=DccoOSyzQef0sN/DvM/vPZqRe9HKJsUE7iEnyyKkXs3CtlEA7LYFXWnUFyn8vTal96vWocb3WeWncN30HJmmg12VO5kUo8kNJj6Ar4lO3Grc8eZxQjppHy7ZythFmyAIm+VvlOF7mR2ubDHMpWbWJnfECKAwFfBc9f6X1ALZWkk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758028133; c=relaxed/simple; bh=pbBzLjrUvy4Jws3IrdIk6Q8D7H91HuRG6YGPY55CtrI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=CinjyMVxXD8SsR0DT52BTV45xtwx5t1zCUsqgWC8YmNXYb7q4lDIZOFlIFhXSLcmjV3GyPhMGfoYJxdxre2RHHSlqMQukcQCJeD3HRMEutEF/HQJbTfipHIIPmbZ3gHr35A0/TfTmzJww6+olIrquayWHuQYgBsPzIs5s51qs+0= 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=GDAjJkIk; 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="GDAjJkIk" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1758028131; 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=6Dg27mnkpxY6+wpImWrHzQoz+UEHN7ql4f1eRWWfzQk=; b=GDAjJkIkLNCwM3/0gKu1THW+iQoWGuhH+eKePMj/GwC8tPVdKa2o9eBlZlPAtyFIrzIgPz T0X2lopNkmpPIWCU+vIt9dP78zK+JWR/vaKBXHKwUJvkBsj6W3KKV3rJXAdtJc4/ebEGjf STUEk0qIO0AIHYJfYYbcWT8x3/8hfhw= 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-621-8UvkHeh8NO2a03xXa6XTLA-1; Tue, 16 Sep 2025 09:08:47 -0400 X-MC-Unique: 8UvkHeh8NO2a03xXa6XTLA-1 X-Mimecast-MFC-AGG-ID: 8UvkHeh8NO2a03xXa6XTLA_1758028126 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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 7D87618002CC; Tue, 16 Sep 2025 13:08:46 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.44.34.12]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id B5F9E1956048; Tue, 16 Sep 2025 13:08:41 +0000 (UTC) From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= To: "Michael S . Tsirkin " Cc: Stefano Garzarella , jasowang@redhat.com, Xuan Zhuo , linux-kernel@vger.kernel.org, Maxime Coquelin , Yongji Xie , Cindy Lu , Laurent Vivier , virtualization@lists.linux.dev, =?UTF-8?q?Eugenio=20P=C3=A9rez?= Subject: [PATCH v2 1/7] vduse: add v1 API definition Date: Tue, 16 Sep 2025 15:08:27 +0200 Message-ID: <20250916130833.329087-2-eperezma@redhat.com> In-Reply-To: <20250916130833.329087-1-eperezma@redhat.com> References: <20250916130833.329087-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.0 on 10.30.177.12 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. 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.51.0 From nobody Thu Oct 2 13:01:36 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 AF53F315D4F for ; Tue, 16 Sep 2025 13:08:57 +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=1758028139; cv=none; b=VrXXd5e3pJMlaCp3nwqJhclQ9JCGy7122agmCXR/ngQfIOUxABKZHK5hmO5bQWKKLkgx27EgpiLiRb9V3tS0krznUHJnoAfD1QPEcNdoG/0zZ3xBzEt+Vb+RE3qNr0bxP/oAEOZYu++O7aoJYUnM5uRorsRAPwsgkLXUGaPQZaQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758028139; c=relaxed/simple; bh=d3fQw6S99FciRy182QOtEgFEi+t/Z1n3HSNtRTk38Y4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=mF8aPR/toK1V8KB8NZxMqSo804B+cIY5a1M28ujZMZGYKuD0eW0agfxKm2j1e0oVnov7rpd0SD63zO+LQ8bXpZo0w9Z1nzbGC+9/Qm6TQl0u2O02iGZTrx4nWX4SnKZE0Q4rtbLMUUD5zwP+A8EE6C0Gu4/vmA7UKhC/4a16TvE= 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=Gk9DccDG; 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="Gk9DccDG" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1758028136; 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=7Z09Es5Ylciu4ZkGEwk7fK8/twSlfHUI3tackVpOb1U=; b=Gk9DccDGLEyM8HvlTPdSZdqMd/ps4uMHadRp01j9AQeaAZqINU+agiY/0plMenDWoM4wzZ RE3s1jeglHuZxiQIGMCCx0nZFyBBcefxALo3Zv1xXYfcQFa28bsaNSiZs68Gjyqu2o7oDl VoyC9vdH/kJzATQmI9YyBVal9/zv0wI= 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-119-WpCKzfMoM9Wcol4Cu9Ufhw-1; Tue, 16 Sep 2025 09:08:52 -0400 X-MC-Unique: WpCKzfMoM9Wcol4Cu9Ufhw-1 X-Mimecast-MFC-AGG-ID: WpCKzfMoM9Wcol4Cu9Ufhw_1758028131 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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 7CFE01800371; Tue, 16 Sep 2025 13:08:51 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.44.34.12]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id E77E119560B8; Tue, 16 Sep 2025 13:08:46 +0000 (UTC) From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= To: "Michael S . Tsirkin " Cc: Stefano Garzarella , jasowang@redhat.com, Xuan Zhuo , linux-kernel@vger.kernel.org, Maxime Coquelin , Yongji Xie , Cindy Lu , Laurent Vivier , virtualization@lists.linux.dev, =?UTF-8?q?Eugenio=20P=C3=A9rez?= Subject: [PATCH v2 2/7] vduse: make domain_lock an rwlock Date: Tue, 16 Sep 2025 15:08:28 +0200 Message-ID: <20250916130833.329087-3-eperezma@redhat.com> In-Reply-To: <20250916130833.329087-1-eperezma@redhat.com> References: <20250916130833.329087-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.0 on 10.30.177.12 It will be used in a few more scenarios read-only so make it more scalable. Suggested-by: Xie Yongji Signed-off-by: Eugenio P=C3=A9rez Acked-by: Jason Wang Reviewed-by: Xie Yongji --- v2: New in v2 --- drivers/vdpa/vdpa_user/vduse_dev.c | 41 +++++++++++++++--------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/drivers/vdpa/vdpa_user/vduse_dev.c b/drivers/vdpa/vdpa_user/vd= use_dev.c index e7bced0b5542..2b6a8958ffe0 100644 --- a/drivers/vdpa/vdpa_user/vduse_dev.c +++ b/drivers/vdpa/vdpa_user/vduse_dev.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -117,7 +118,7 @@ struct vduse_dev { struct vduse_umem *umem; struct mutex mem_lock; unsigned int bounce_size; - struct mutex domain_lock; + rwlock_t domain_lock; }; =20 struct vduse_dev_msg { @@ -1176,9 +1177,9 @@ static long vduse_dev_ioctl(struct file *file, unsign= ed int cmd, if (entry.start > entry.last) break; =20 - mutex_lock(&dev->domain_lock); + read_lock(&dev->domain_lock); if (!dev->domain) { - mutex_unlock(&dev->domain_lock); + read_unlock(&dev->domain_lock); break; } spin_lock(&dev->domain->iotlb_lock); @@ -1193,7 +1194,7 @@ static long vduse_dev_ioctl(struct file *file, unsign= ed int cmd, entry.perm =3D map->perm; } spin_unlock(&dev->domain->iotlb_lock); - mutex_unlock(&dev->domain_lock); + read_unlock(&dev->domain_lock); ret =3D -EINVAL; if (!f) break; @@ -1346,10 +1347,10 @@ static long vduse_dev_ioctl(struct file *file, unsi= gned int cmd, sizeof(umem.reserved))) break; =20 - mutex_lock(&dev->domain_lock); + write_lock(&dev->domain_lock); ret =3D vduse_dev_reg_umem(dev, umem.iova, umem.uaddr, umem.size); - mutex_unlock(&dev->domain_lock); + write_unlock(&dev->domain_lock); break; } case VDUSE_IOTLB_DEREG_UMEM: { @@ -1363,10 +1364,10 @@ static long vduse_dev_ioctl(struct file *file, unsi= gned int cmd, if (!is_mem_zero((const char *)umem.reserved, sizeof(umem.reserved))) break; - mutex_lock(&dev->domain_lock); + write_lock(&dev->domain_lock); ret =3D vduse_dev_dereg_umem(dev, umem.iova, umem.size); - mutex_unlock(&dev->domain_lock); + write_unlock(&dev->domain_lock); break; } case VDUSE_IOTLB_GET_INFO: { @@ -1385,9 +1386,9 @@ static long vduse_dev_ioctl(struct file *file, unsign= ed int cmd, sizeof(info.reserved))) break; =20 - mutex_lock(&dev->domain_lock); + read_lock(&dev->domain_lock); if (!dev->domain) { - mutex_unlock(&dev->domain_lock); + read_unlock(&dev->domain_lock); break; } spin_lock(&dev->domain->iotlb_lock); @@ -1402,7 +1403,7 @@ static long vduse_dev_ioctl(struct file *file, unsign= ed int cmd, info.capability |=3D VDUSE_IOVA_CAP_UMEM; } spin_unlock(&dev->domain->iotlb_lock); - mutex_unlock(&dev->domain_lock); + read_unlock(&dev->domain_lock); if (!map) break; =20 @@ -1425,10 +1426,10 @@ static int vduse_dev_release(struct inode *inode, s= truct file *file) { struct vduse_dev *dev =3D file->private_data; =20 - mutex_lock(&dev->domain_lock); + write_lock(&dev->domain_lock); if (dev->domain) vduse_dev_dereg_umem(dev, 0, dev->domain->bounce_size); - mutex_unlock(&dev->domain_lock); + write_unlock(&dev->domain_lock); spin_lock(&dev->msg_lock); /* Make sure the inflight messages can processed after reconncection */ list_splice_init(&dev->recv_list, &dev->send_list); @@ -1647,7 +1648,7 @@ static struct vduse_dev *vduse_dev_create(void) =20 mutex_init(&dev->lock); mutex_init(&dev->mem_lock); - mutex_init(&dev->domain_lock); + rwlock_init(&dev->domain_lock); spin_lock_init(&dev->msg_lock); INIT_LIST_HEAD(&dev->send_list); INIT_LIST_HEAD(&dev->recv_list); @@ -1805,7 +1806,7 @@ static ssize_t bounce_size_store(struct device *devic= e, int ret; =20 ret =3D -EPERM; - mutex_lock(&dev->domain_lock); + write_lock(&dev->domain_lock); if (dev->domain) goto unlock; =20 @@ -1821,7 +1822,7 @@ static ssize_t bounce_size_store(struct device *devic= e, dev->bounce_size =3D bounce_size & PAGE_MASK; ret =3D count; unlock: - mutex_unlock(&dev->domain_lock); + write_unlock(&dev->domain_lock); return ret; } =20 @@ -2045,11 +2046,11 @@ static int vdpa_dev_add(struct vdpa_mgmt_dev *mdev,= const char *name, if (ret) return ret; =20 - mutex_lock(&dev->domain_lock); + write_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); + write_unlock(&dev->domain_lock); if (!dev->domain) { put_device(&dev->vdev->vdpa.dev); return -ENOMEM; @@ -2059,10 +2060,10 @@ static int vdpa_dev_add(struct vdpa_mgmt_dev *mdev,= const char *name, ret =3D _vdpa_register_device(&dev->vdev->vdpa, dev->vq_num); if (ret) { put_device(&dev->vdev->vdpa.dev); - mutex_lock(&dev->domain_lock); + write_lock(&dev->domain_lock); vduse_domain_destroy(dev->domain); dev->domain =3D NULL; - mutex_unlock(&dev->domain_lock); + write_unlock(&dev->domain_lock); return ret; } =20 --=20 2.51.0 From nobody Thu Oct 2 13:01:36 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 6E180317708 for ; Tue, 16 Sep 2025 13:09:02 +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=1758028144; cv=none; b=WzoMjQlxl5Wa+ORXRjXnpryoegGoHjxKa4mpY6QKDp8Tocz8I3BvLOcubHcvDVI/e0PNQpz4wdyV08x5Hb4peOSiYhNzccJ9WOi0YwPzhg5qKvjE2+z/0uK0fT8eozXdtS2opC+6KuNOUVc7P2X/eO8ovd8XR7wNbSG4WEi7lE8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758028144; c=relaxed/simple; bh=e3LN2q1z3ZNE8/jhSjnwlrdsuZG4OB889I3o1t6X91c=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=n4ImVLilI2WN8GshQa0yP+dMWPMyM5zPpIMigwOOUi0MAtgjCp+MooD2IKbaHyg/OPOclWu6olZcE6yKvQiGgz0HfdNnDLvB7/KUnGO6VLj23jSaFCyLqiYpZOKpNwvdhXOGXNps3mboXUCMNQE1KbvUZhHfP5Kw8Y6U/9N0Xl8= 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=hmUOmYtX; 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="hmUOmYtX" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1758028141; 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=DSZ4OcSH4CccCukm8EV0Syqz+0GmL3zIMilMsekCcsU=; b=hmUOmYtXG27vJBBysFMW0j+IdHzcKWro8yqnbIkNBMS1hIokfhebxwMfgutXMDlqY3MTHS azr567X5gGfG//EE1XbZD30m9EQcQso/i7VXawZTxEHMnqECwUdoUhAskS6T9gJvidXUa3 +PGJIuQn4U7hkqRdkptk2lOeK5MGYe8= 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-223-sBVAxZmMNj6K9Uq75uyqsQ-1; Tue, 16 Sep 2025 09:08:58 -0400 X-MC-Unique: sBVAxZmMNj6K9Uq75uyqsQ-1 X-Mimecast-MFC-AGG-ID: sBVAxZmMNj6K9Uq75uyqsQ_1758028137 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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 BBF2218005B3; Tue, 16 Sep 2025 13:08:56 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.44.34.12]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 551961956048; Tue, 16 Sep 2025 13:08:51 +0000 (UTC) From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= To: "Michael S . Tsirkin " Cc: Stefano Garzarella , jasowang@redhat.com, Xuan Zhuo , linux-kernel@vger.kernel.org, Maxime Coquelin , Yongji Xie , Cindy Lu , Laurent Vivier , virtualization@lists.linux.dev, =?UTF-8?q?Eugenio=20P=C3=A9rez?= Subject: [PATCH v2 3/7] vduse: add vq group support Date: Tue, 16 Sep 2025 15:08:29 +0200 Message-ID: <20250916130833.329087-4-eperezma@redhat.com> In-Reply-To: <20250916130833.329087-1-eperezma@redhat.com> References: <20250916130833.329087-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.0 on 10.30.177.12 This allows sepparate 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. Signed-off-by: Eugenio P=C3=A9rez --- 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 | 37 ++++++++++++++++++++++++++---- include/uapi/linux/vduse.h | 12 +++++++--- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/drivers/vdpa/vdpa_user/vduse_dev.c b/drivers/vdpa/vdpa_user/vd= use_dev.c index 2b6a8958ffe0..42f8807911d4 100644 --- a/drivers/vdpa/vdpa_user/vduse_dev.c +++ b/drivers/vdpa/vdpa_user/vduse_dev.c @@ -59,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; @@ -115,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; @@ -593,6 +595,13 @@ 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); + + return dev->vqs[idx]->vq_group; +} + static int vduse_vdpa_get_vq_state(struct vdpa_device *vdpa, u16 idx, struct vdpa_vq_state *state) { @@ -790,6 +799,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, @@ -1253,12 +1263,21 @@ 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 && + config.group > dev->ngroups) + 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; } @@ -1738,12 +1757,19 @@ 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 > 0xffff) + return false; + if (config->vq_align > PAGE_SIZE) return false; =20 @@ -1859,6 +1885,7 @@ 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 < 1) ? 1 : (config->ngroups ?: 1); dev->name =3D kstrdup(config->name, GFP_KERNEL); if (!dev->name) goto err_str; @@ -1937,7 +1964,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); @@ -2018,7 +2045,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.51.0 From nobody Thu Oct 2 13:01:36 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 70DCC315D4F for ; Tue, 16 Sep 2025 13:09:05 +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=1758028147; cv=none; b=pewXLULGmDeCZiKS68DKFsHtjq3rJ/hRmrXy2LZFFMco8YYI2BDCwCx03RY31NbUEAzqsjEGn/9H8+9sAhe9AU6j4RlQNt3co1ZBFXJ/TH6+dIXhyW2itTgCKvtcC6ZCuhdInbwAfCeSk9EOrhCLUFisrdyeiwKXYpZmSak3D9c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758028147; c=relaxed/simple; bh=zMKnyo4BN62BGD/izcadd9bdD37sncHp2t+/amkV5dM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=a/FiAzZZvRLMo8Sk8yRi/1vUpFeXNQplOrYAFXsPVphcxT/YZqwB7uDXhXXbrZjf6K5etxqC0Ib5/akKeeIKXVfHBS25PNh8LpqQZ6MaTlfGJGoOPZFv/7UpcZXmGalVoRMbU2axpdXR/uvQx/UUJf0VreBW1FoeuCtJ7ECZFsg= 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=cIZa3K8f; 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="cIZa3K8f" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1758028144; 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=ggikySRi5r73RkJ4ALYw3GYJq0PuR1mbjE1EBaHQaMA=; b=cIZa3K8ffSJYZ3qprZgg+MZluACpz32q/ZQ6JlBiPAAshf4v/aqT93FGz5LeVU6UZy0RnN YS8+62fGScdjhli3zUirDA3dxPKFV+pgxFSR+hQ5dGqUY7CI60LZzwNM0LWBn8pJk9HIih wv38IMZGaGH2PS4cUoIVBcm+NzmyThk= 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-367-pjDG12efPxKZv4DlrkMKGg-1; Tue, 16 Sep 2025 09:09:02 -0400 X-MC-Unique: pjDG12efPxKZv4DlrkMKGg-1 X-Mimecast-MFC-AGG-ID: pjDG12efPxKZv4DlrkMKGg_1758028142 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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 B3523180058A; Tue, 16 Sep 2025 13:09:01 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.44.34.12]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 5165819560B8; Tue, 16 Sep 2025 13:08:57 +0000 (UTC) From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= To: "Michael S . Tsirkin " Cc: Stefano Garzarella , jasowang@redhat.com, Xuan Zhuo , linux-kernel@vger.kernel.org, Maxime Coquelin , Yongji Xie , Cindy Lu , Laurent Vivier , virtualization@lists.linux.dev, =?UTF-8?q?Eugenio=20P=C3=A9rez?= Subject: [PATCH v2 4/7] vduse: return internal vq group struct as map token Date: Tue, 16 Sep 2025 15:08:30 +0200 Message-ID: <20250916130833.329087-5-eperezma@redhat.com> In-Reply-To: <20250916130833.329087-1-eperezma@redhat.com> References: <20250916130833.329087-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.0 on 10.30.177.12 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 Signed-off-by: Eugenio P=C3=A9rez --- RFC v3: * Make the vq groups a dynamic array to support an arbitrary number of them. --- drivers/vdpa/vdpa_user/vduse_dev.c | 52 ++++++++++++++++++++++++------ include/linux/virtio.h | 6 ++-- 2 files changed, 46 insertions(+), 12 deletions(-) diff --git a/drivers/vdpa/vdpa_user/vduse_dev.c b/drivers/vdpa/vdpa_user/vd= use_dev.c index 42f8807911d4..9c12ae72abc2 100644 --- a/drivers/vdpa/vdpa_user/vduse_dev.c +++ b/drivers/vdpa/vdpa_user/vduse_dev.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -85,6 +86,10 @@ struct vduse_umem { struct mm_struct *mm; }; =20 +struct vduse_vq_group_int { + 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_int *groups; struct mutex mem_lock; unsigned int bounce_size; rwlock_t domain_lock; @@ -602,6 +608,15 @@ 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 dev->vqs[idx]->vq_group; + 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) { @@ -822,6 +837,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 @@ -829,7 +845,8 @@ static void vduse_dev_sync_single_for_device(union virt= io_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 =3D token.group->dev; + struct vduse_iova_domain *domain =3D vdev->domain; =20 vduse_domain_sync_single_for_device(domain, dma_addr, size, dir); } @@ -838,7 +855,8 @@ 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 =3D token.group->dev; + struct vduse_iova_domain *domain =3D vdev->domain; =20 vduse_domain_sync_single_for_cpu(domain, dma_addr, size, dir); } @@ -848,7 +866,8 @@ static dma_addr_t vduse_dev_map_page(union virtio_map t= oken, struct page *page, enum dma_data_direction dir, unsigned long attrs) { - struct vduse_iova_domain *domain =3D token.iova_domain; + struct vduse_dev *vdev =3D token.group->dev; + struct vduse_iova_domain *domain =3D vdev->domain; =20 return vduse_domain_map_page(domain, page, offset, size, dir, attrs); } @@ -857,7 +876,8 @@ static void vduse_dev_unmap_page(union virtio_map token= , 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 =3D token.group->dev; + struct vduse_iova_domain *domain =3D vdev->domain; =20 return vduse_domain_unmap_page(domain, dma_addr, size, dir, attrs); } @@ -865,7 +885,8 @@ static void vduse_dev_unmap_page(union virtio_map token= , 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 =3D token.group->dev; + struct vduse_iova_domain *domain =3D vdev->domain; unsigned long iova; void *addr; =20 @@ -884,14 +905,16 @@ 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 =3D token.group->dev; + struct vduse_iova_domain *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 =3D token.group->dev; + struct vduse_iova_domain *domain =3D vdev->domain; =20 return dma_addr < domain->bounce_size; } @@ -905,7 +928,8 @@ static int vduse_dev_mapping_error(union virtio_map tok= en, 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 =3D token.group->dev; + struct vduse_iova_domain *domain =3D vdev->domain; =20 return domain->bounce_size; } @@ -1720,6 +1744,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 @@ -1885,7 +1910,15 @@ 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 < 1) ? 1 : (config->ngroups ?: 1); + 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; @@ -1922,6 +1955,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; @@ -2083,7 +2118,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 96c66126c074..5f8db75f7833 100644 --- a/include/linux/virtio.h +++ b/include/linux/virtio.h @@ -41,13 +41,13 @@ struct virtqueue { void *priv; }; =20 -struct vduse_iova_domain; +struct vduse_vq_group_int; =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_int *group; }; =20 int virtqueue_add_outbuf(struct virtqueue *vq, --=20 2.51.0 From nobody Thu Oct 2 13:01:36 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 E4D90315D4F for ; Tue, 16 Sep 2025 13:09:11 +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=1758028153; cv=none; b=FWMOjAvWSnHAi94hXUKjwd6m7tavnZycSxqqDfk7t1EX2HA31LLHGFOoVgli7q5zUuRHvbKulgGy6RCdoW0Z3vg8YeE51jMYTvGxeDB5bI4T3H9YKOvL3a1b3LyvyqOoc2nWo0hbmsYzc5hFrmNZ13YRkLQSF7kE83cdeSqJ5SA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758028153; c=relaxed/simple; bh=dliCQynGAe/gHAOPKDRtB9YPRIonGYk4YALBzZAI21c=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Zk7YwpTPNynZZeT4DcdCUNdIj8IeBYS+JhNCej5ah5xvIN9FbIej2evCfQs46xb9xnn2J/OyhpuX53Nu+AeZaO4/uatxRLImoK3r/gm01++fTXSwYDiyEka2AcePpsgoD/JvJfAwLkHsvxtcD96jXqT5OQgXC2D8mvBEvMlZLxI= 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=GIYJQJr0; 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="GIYJQJr0" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1758028150; 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=uixg72Jfkxev7ts5faGYDDwpmN8VEFxGF3BrCV5rlv0=; b=GIYJQJr0tTTYimi4CGew/ZaWWnHJX4q30olJhEBJwPWlyydGarQW4zD847HkxUof1mZXll k9pkk/DUNCRUPEMAv64pspPtMfAKvrrbkH5pUwiaRDAOub+ZyOgX3ozo75bI8I0LZTy2d0 rwGDiGS8XTXONnMMoWqnwu1YIGqYqss= 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-52-Q0xQhW6PP_-YAzBi9gm63g-1; Tue, 16 Sep 2025 09:09:07 -0400 X-MC-Unique: Q0xQhW6PP_-YAzBi9gm63g-1 X-Mimecast-MFC-AGG-ID: Q0xQhW6PP_-YAzBi9gm63g_1758028146 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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 72A191800291; Tue, 16 Sep 2025 13:09:06 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.44.34.12]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 5428A19560B8; Tue, 16 Sep 2025 13:09:02 +0000 (UTC) From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= To: "Michael S . Tsirkin " Cc: Stefano Garzarella , jasowang@redhat.com, Xuan Zhuo , linux-kernel@vger.kernel.org, Maxime Coquelin , Yongji Xie , Cindy Lu , Laurent Vivier , virtualization@lists.linux.dev, =?UTF-8?q?Eugenio=20P=C3=A9rez?= Subject: [PATCH v2 5/7] vduse: create vduse_as to make it an array Date: Tue, 16 Sep 2025 15:08:31 +0200 Message-ID: <20250916130833.329087-6-eperezma@redhat.com> In-Reply-To: <20250916130833.329087-1-eperezma@redhat.com> References: <20250916130833.329087-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.0 on 10.30.177.12 This is a first step so we can make more than one different address spaces. No change on the colde flow intended. Acked-by: Jason Wang Signed-off-by: Eugenio P=C3=A9rez --- drivers/vdpa/vdpa_user/vduse_dev.c | 114 +++++++++++++++-------------- 1 file changed, 59 insertions(+), 55 deletions(-) diff --git a/drivers/vdpa/vdpa_user/vduse_dev.c b/drivers/vdpa/vdpa_user/vd= use_dev.c index 9c12ae72abc2..b45b1d22784f 100644 --- a/drivers/vdpa/vdpa_user/vduse_dev.c +++ b/drivers/vdpa/vdpa_user/vduse_dev.c @@ -86,6 +86,12 @@ 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_int { struct vduse_dev *dev; }; @@ -94,7 +100,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 +128,7 @@ struct vduse_dev { u32 vq_num; u32 vq_align; u32 ngroups; - struct vduse_umem *umem; struct vduse_vq_group_int *groups; - struct mutex mem_lock; unsigned int bounce_size; rwlock_t domain_lock; }; @@ -439,7 +443,7 @@ static __poll_t vduse_dev_poll(struct file *file, poll_= table *wait) static void vduse_dev_reset(struct vduse_dev *dev) { int i; - struct vduse_iova_domain *domain =3D dev->domain; + struct vduse_iova_domain *domain =3D dev->as.domain; =20 /* The coherent mappings are handled in vduse_dev_free_coherent() */ if (domain && domain->bounce_map) @@ -788,13 +792,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.domain, iotlb); if (ret) return ret; =20 ret =3D vduse_dev_update_iotlb(dev, 0ULL, ULLONG_MAX); if (ret) { - vduse_domain_clear_map(dev->domain, iotlb); + vduse_domain_clear_map(dev->as.domain, iotlb); return ret; } =20 @@ -846,7 +850,7 @@ static void vduse_dev_sync_single_for_device(union virt= io_map token, enum dma_data_direction dir) { struct vduse_dev *vdev =3D token.group->dev; - struct vduse_iova_domain *domain =3D vdev->domain; + struct vduse_iova_domain *domain =3D vdev->as.domain; =20 vduse_domain_sync_single_for_device(domain, dma_addr, size, dir); } @@ -856,7 +860,7 @@ static void vduse_dev_sync_single_for_cpu(union virtio_= map token, enum dma_data_direction dir) { struct vduse_dev *vdev =3D token.group->dev; - struct vduse_iova_domain *domain =3D vdev->domain; + struct vduse_iova_domain *domain =3D vdev->as.domain; =20 vduse_domain_sync_single_for_cpu(domain, dma_addr, size, dir); } @@ -867,7 +871,7 @@ static dma_addr_t vduse_dev_map_page(union virtio_map t= oken, struct page *page, unsigned long attrs) { struct vduse_dev *vdev =3D token.group->dev; - struct vduse_iova_domain *domain =3D vdev->domain; + struct vduse_iova_domain *domain =3D vdev->as.domain; =20 return vduse_domain_map_page(domain, page, offset, size, dir, attrs); } @@ -877,7 +881,7 @@ static void vduse_dev_unmap_page(union virtio_map token= , dma_addr_t dma_addr, unsigned long attrs) { struct vduse_dev *vdev =3D token.group->dev; - struct vduse_iova_domain *domain =3D vdev->domain; + struct vduse_iova_domain *domain =3D vdev->as.domain; =20 return vduse_domain_unmap_page(domain, dma_addr, size, dir, attrs); } @@ -886,7 +890,7 @@ 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 =3D token.group->dev; - struct vduse_iova_domain *domain =3D vdev->domain; + struct vduse_iova_domain *domain =3D vdev->as.domain; unsigned long iova; void *addr; =20 @@ -906,7 +910,7 @@ static void vduse_dev_free_coherent(union virtio_map to= ken, size_t size, unsigned long attrs) { struct vduse_dev *vdev =3D token.group->dev; - struct vduse_iova_domain *domain =3D vdev->domain; + struct vduse_iova_domain *domain =3D vdev->as.domain; =20 vduse_domain_free_coherent(domain, size, vaddr, dma_addr, attrs); } @@ -914,7 +918,7 @@ static void vduse_dev_free_coherent(union virtio_map to= ken, size_t size, static bool vduse_dev_need_sync(union virtio_map token, dma_addr_t dma_add= r) { struct vduse_dev *vdev =3D token.group->dev; - struct vduse_iova_domain *domain =3D vdev->domain; + struct vduse_iova_domain *domain =3D vdev->as.domain; =20 return dma_addr < domain->bounce_size; } @@ -929,7 +933,7 @@ static int vduse_dev_mapping_error(union virtio_map tok= en, dma_addr_t dma_addr) static size_t vduse_dev_max_mapping_size(union virtio_map token) { struct vduse_dev *vdev =3D token.group->dev; - struct vduse_iova_domain *domain =3D vdev->domain; + struct vduse_iova_domain *domain =3D vdev->as.domain; =20 return domain->bounce_size; } @@ -1076,29 +1080,29 @@ static int vduse_dev_dereg_umem(struct vduse_dev *d= ev, { int ret; =20 - mutex_lock(&dev->mem_lock); + mutex_lock(&dev->as.mem_lock); ret =3D -ENOENT; - if (!dev->umem) + if (!dev->as.umem) goto unlock; =20 ret =3D -EINVAL; - if (!dev->domain) + if (!dev->as.domain) goto unlock; =20 - if (dev->umem->iova !=3D iova || size !=3D dev->domain->bounce_size) + if (dev->as.umem->iova !=3D iova || size !=3D dev->as.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.domain); + unpin_user_pages_dirty_lock(dev->as.umem->pages, + dev->as.umem->npages, true); + atomic64_sub(dev->as.umem->npages, &dev->as.umem->mm->pinned_vm); + mmdrop(dev->as.umem->mm); + vfree(dev->as.umem->pages); + kfree(dev->as.umem); + dev->as.umem =3D NULL; ret =3D 0; unlock: - mutex_unlock(&dev->mem_lock); + mutex_unlock(&dev->as.mem_lock); return ret; } =20 @@ -1111,14 +1115,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.domain || !dev->as.domain->bounce_map || + size !=3D dev->as.domain->bounce_size || iova !=3D 0 || uaddr & ~PAGE_MASK) return -EINVAL; =20 - mutex_lock(&dev->mem_lock); + mutex_lock(&dev->as.mem_lock); ret =3D -EEXIST; - if (dev->umem) + if (dev->as.umem) goto unlock; =20 ret =3D -ENOMEM; @@ -1142,7 +1146,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.domain, page_list, pinned); if (ret) goto out; @@ -1155,7 +1159,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.umem =3D umem; out: if (ret && pinned > 0) unpin_user_pages(page_list, pinned); @@ -1166,7 +1170,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.mem_lock); return ret; } =20 @@ -1212,12 +1216,12 @@ static long vduse_dev_ioctl(struct file *file, unsi= gned int cmd, break; =20 read_lock(&dev->domain_lock); - if (!dev->domain) { + if (!dev->as.domain) { read_unlock(&dev->domain_lock); break; } - spin_lock(&dev->domain->iotlb_lock); - map =3D vhost_iotlb_itree_first(dev->domain->iotlb, + spin_lock(&dev->as.domain->iotlb_lock); + map =3D vhost_iotlb_itree_first(dev->as.domain->iotlb, entry.start, entry.last); if (map) { map_file =3D (struct vdpa_map_file *)map->opaque; @@ -1227,7 +1231,7 @@ static long vduse_dev_ioctl(struct file *file, unsign= ed int cmd, entry.last =3D map->last; entry.perm =3D map->perm; } - spin_unlock(&dev->domain->iotlb_lock); + spin_unlock(&dev->as.domain->iotlb_lock); read_unlock(&dev->domain_lock); ret =3D -EINVAL; if (!f) @@ -1430,22 +1434,22 @@ static long vduse_dev_ioctl(struct file *file, unsi= gned int cmd, break; =20 read_lock(&dev->domain_lock); - if (!dev->domain) { + if (!dev->as.domain) { read_unlock(&dev->domain_lock); break; } - spin_lock(&dev->domain->iotlb_lock); - map =3D vhost_iotlb_itree_first(dev->domain->iotlb, + spin_lock(&dev->as.domain->iotlb_lock); + map =3D vhost_iotlb_itree_first(dev->as.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.domain->bounce_map && map->start =3D=3D 0 && + map->last =3D=3D dev->as.domain->bounce_size - 1) info.capability |=3D VDUSE_IOVA_CAP_UMEM; } - spin_unlock(&dev->domain->iotlb_lock); + spin_unlock(&dev->as.domain->iotlb_lock); read_unlock(&dev->domain_lock); if (!map) break; @@ -1470,8 +1474,8 @@ static int vduse_dev_release(struct inode *inode, str= uct file *file) struct vduse_dev *dev =3D file->private_data; =20 write_lock(&dev->domain_lock); - if (dev->domain) - vduse_dev_dereg_umem(dev, 0, dev->domain->bounce_size); + if (dev->as.domain) + vduse_dev_dereg_umem(dev, 0, dev->as.domain->bounce_size); write_unlock(&dev->domain_lock); spin_lock(&dev->msg_lock); /* Make sure the inflight messages can processed after reconncection */ @@ -1690,7 +1694,7 @@ static struct vduse_dev *vduse_dev_create(void) return NULL; =20 mutex_init(&dev->lock); - mutex_init(&dev->mem_lock); + mutex_init(&dev->as.mem_lock); rwlock_init(&dev->domain_lock); spin_lock_init(&dev->msg_lock); INIT_LIST_HEAD(&dev->send_list); @@ -1741,8 +1745,8 @@ 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); + if (dev->as.domain) + vduse_domain_destroy(dev->as.domain); kfree(dev->name); kfree(dev->groups); vduse_dev_destroy(dev); @@ -1858,7 +1862,7 @@ static ssize_t bounce_size_store(struct device *devic= e, =20 ret =3D -EPERM; write_lock(&dev->domain_lock); - if (dev->domain) + if (dev->as.domain) goto unlock; =20 ret =3D kstrtouint(buf, 10, &bounce_size); @@ -2109,11 +2113,11 @@ static int vdpa_dev_add(struct vdpa_mgmt_dev *mdev,= const char *name, return ret; =20 write_lock(&dev->domain_lock); - if (!dev->domain) - dev->domain =3D vduse_domain_create(VDUSE_IOVA_SIZE - 1, + if (!dev->as.domain) + dev->as.domain =3D vduse_domain_create(VDUSE_IOVA_SIZE - 1, dev->bounce_size); write_unlock(&dev->domain_lock); - if (!dev->domain) { + if (!dev->as.domain) { put_device(&dev->vdev->vdpa.dev); return -ENOMEM; } @@ -2122,8 +2126,8 @@ static int vdpa_dev_add(struct vdpa_mgmt_dev *mdev, c= onst char *name, if (ret) { put_device(&dev->vdev->vdpa.dev); write_lock(&dev->domain_lock); - vduse_domain_destroy(dev->domain); - dev->domain =3D NULL; + vduse_domain_destroy(dev->as.domain); + dev->as.domain =3D NULL; write_unlock(&dev->domain_lock); return ret; } --=20 2.51.0 From nobody Thu Oct 2 13:01:36 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 4012F31B829 for ; Tue, 16 Sep 2025 13:09:14 +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=1758028158; cv=none; b=CmjvK13krjJcBPmWRjTm8Dzoo0yp2ikfGhObdqmAUcwCLcbXgqAFjIzx8o6xrTrDCy7uKL1W5wjLKWCjddhJeBdXaIDpWLcaVE8gvfgHbfHsv5OtxrZl3yvij/DrVqrXnY0in61ugOtDrs+lAoLsiClbZbC+jMquJLFztj4jx8U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758028158; c=relaxed/simple; bh=pElcS0V3HcbTQQFA/xyIL9b+m8iDSg7+dJR9dHNiquQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=UuYMhP/zJ09vTFMSujx2NwNvQQyLKzu7T0ZWj1fXwUR9p5UEHkkIRZZQNBfDdLOBE+kaEPMVbLzoyXOm45Tbr6K6KAv/UXeHuVKFKhip8sr/6cwjyIOvcsXZP2MAMHG5lYH1uo4JrTLRCNothMLyJzcfSYgJCgYRVFl+iC0tzhE= 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=S9O6UlqT; 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="S9O6UlqT" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1758028154; 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=bXJFBugO8ce9P4XICzVJD+2D+4lrgROr3/cZ66RlYzQ=; b=S9O6UlqTPPt8DQB8HCIO0E4Q5gBtSxMirUqcmBtXhrDkN8jEaGlVTiy57siK5mkaMD7Pdk b0UPvsbrgynrvmdaR0M7QkYXYD+YmjBuOzhXxWWZqsSudRGz7BzQm8pisezjeRss6YiOKv 4iCdLNo5yyZfJnoLRNoErfBvX2cgd6g= 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-191-5LDZ8O_EP2SKbtKIqHEwWQ-1; Tue, 16 Sep 2025 09:09:12 -0400 X-MC-Unique: 5LDZ8O_EP2SKbtKIqHEwWQ-1 X-Mimecast-MFC-AGG-ID: 5LDZ8O_EP2SKbtKIqHEwWQ_1758028151 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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 BAE221800378; Tue, 16 Sep 2025 13:09:11 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.44.34.12]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 0D7BC19560B8; Tue, 16 Sep 2025 13:09:06 +0000 (UTC) From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= To: "Michael S . Tsirkin " Cc: Stefano Garzarella , jasowang@redhat.com, Xuan Zhuo , linux-kernel@vger.kernel.org, Maxime Coquelin , Yongji Xie , Cindy Lu , Laurent Vivier , virtualization@lists.linux.dev, =?UTF-8?q?Eugenio=20P=C3=A9rez?= Subject: [PATCH v2 6/7] vduse: add vq group asid support Date: Tue, 16 Sep 2025 15:08:32 +0200 Message-ID: <20250916130833.329087-7-eperezma@redhat.com> In-Reply-To: <20250916130833.329087-1-eperezma@redhat.com> References: <20250916130833.329087-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.0 on 10.30.177.12 Add support for assigning Address Space Identifiers (ASIDs) to each VQ group. This enables mapping each group into a distinct memory space. Now that the driver can change ASID in the middle of operation, the domain that each vq address point is also protected by domain_lock. Signed-off-by: Eugenio P=C3=A9rez --- 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 | 324 +++++++++++++++++++++-------- include/uapi/linux/vduse.h | 51 ++++- 2 files changed, 284 insertions(+), 91 deletions(-) diff --git a/drivers/vdpa/vdpa_user/vduse_dev.c b/drivers/vdpa/vdpa_user/vd= use_dev.c index b45b1d22784f..06b7790380b7 100644 --- a/drivers/vdpa/vdpa_user/vduse_dev.c +++ b/drivers/vdpa/vdpa_user/vduse_dev.c @@ -93,6 +93,7 @@ struct vduse_as { }; =20 struct vduse_vq_group_int { + struct vduse_iova_domain *domain; struct vduse_dev *dev; }; =20 @@ -100,7 +101,7 @@ struct vduse_dev { struct vduse_vdpa *vdev; struct device *dev; struct vduse_virtqueue **vqs; - struct vduse_as as; + struct vduse_as *as; char *name; struct mutex lock; spinlock_t msg_lock; @@ -128,6 +129,7 @@ struct vduse_dev { u32 vq_num; u32 vq_align; u32 ngroups; + u32 nas; struct vduse_vq_group_int *groups; unsigned int bounce_size; rwlock_t domain_lock; @@ -318,7 +320,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 }; @@ -327,8 +329,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); } @@ -440,14 +448,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->domain_lock); + dev->groups[group].domain =3D dev->as[asid].domain; + write_unlock(&dev->domain_lock); +} + static void vduse_dev_reset(struct vduse_dev *dev) { int i; - struct vduse_iova_domain *domain =3D dev->as.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 @@ -621,6 +644,29 @@ 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) + 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) { @@ -792,13 +838,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->as.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->as.domain, iotlb); + vduse_domain_clear_map(dev->as[asid].domain, iotlb); return ret; } =20 @@ -841,6 +887,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, }; @@ -850,9 +897,12 @@ static void vduse_dev_sync_single_for_device(union vir= tio_map token, enum dma_data_direction dir) { struct vduse_dev *vdev =3D token.group->dev; - struct vduse_iova_domain *domain =3D vdev->as.domain; + struct vduse_iova_domain *domain; =20 + read_lock(&vdev->domain_lock); + domain =3D token.group->domain; vduse_domain_sync_single_for_device(domain, dma_addr, size, dir); + read_unlock(&vdev->domain_lock); } =20 static void vduse_dev_sync_single_for_cpu(union virtio_map token, @@ -860,9 +910,12 @@ static void vduse_dev_sync_single_for_cpu(union virtio= _map token, enum dma_data_direction dir) { struct vduse_dev *vdev =3D token.group->dev; - struct vduse_iova_domain *domain =3D vdev->as.domain; + struct vduse_iova_domain *domain; =20 + read_lock(&vdev->domain_lock); + domain =3D token.group->domain; vduse_domain_sync_single_for_cpu(domain, dma_addr, size, dir); + read_unlock(&vdev->domain_lock); } =20 static dma_addr_t vduse_dev_map_page(union virtio_map token, struct page *= page, @@ -871,9 +924,15 @@ static dma_addr_t vduse_dev_map_page(union virtio_map = token, struct page *page, unsigned long attrs) { struct vduse_dev *vdev =3D token.group->dev; - struct vduse_iova_domain *domain =3D vdev->as.domain; + struct vduse_iova_domain *domain; + dma_addr_t r; =20 - return vduse_domain_map_page(domain, page, offset, size, dir, attrs); + read_lock(&vdev->domain_lock); + domain =3D token.group->domain; + r =3D vduse_domain_map_page(domain, page, offset, size, dir, attrs); + read_unlock(&vdev->domain_lock); + + return r; } =20 static void vduse_dev_unmap_page(union virtio_map token, dma_addr_t dma_ad= dr, @@ -881,27 +940,31 @@ static void vduse_dev_unmap_page(union virtio_map tok= en, dma_addr_t dma_addr, unsigned long attrs) { struct vduse_dev *vdev =3D token.group->dev; - struct vduse_iova_domain *domain =3D vdev->as.domain; + struct vduse_iova_domain *domain; =20 - return vduse_domain_unmap_page(domain, dma_addr, size, dir, attrs); + read_lock(&vdev->domain_lock); + domain =3D token.group->domain; + vduse_domain_unmap_page(domain, dma_addr, size, dir, attrs); + read_unlock(&vdev->domain_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 =3D token.group->dev; - struct vduse_iova_domain *domain =3D vdev->as.domain; + struct vduse_iova_domain *domain; unsigned long iova; - void *addr; + void *addr =3D NULL; =20 *dma_addr =3D DMA_MAPPING_ERROR; + read_lock(&vdev->domain_lock); + domain =3D token.group->domain; addr =3D vduse_domain_alloc_coherent(domain, size, (dma_addr_t *)&iova, flag); - if (!addr) - return NULL; - - *dma_addr =3D (dma_addr_t)iova; + if (addr) + *dma_addr =3D (dma_addr_t)iova; =20 + read_unlock(&vdev->domain_lock); return addr; } =20 @@ -910,17 +973,26 @@ static void vduse_dev_free_coherent(union virtio_map = token, size_t size, unsigned long attrs) { struct vduse_dev *vdev =3D token.group->dev; - struct vduse_iova_domain *domain =3D vdev->as.domain; + struct vduse_iova_domain *domain; =20 + read_lock(&vdev->domain_lock); + domain =3D token.group->domain; vduse_domain_free_coherent(domain, size, vaddr, dma_addr, attrs); + read_unlock(&vdev->domain_lock); } =20 static bool vduse_dev_need_sync(union virtio_map token, dma_addr_t dma_add= r) { struct vduse_dev *vdev =3D token.group->dev; - struct vduse_iova_domain *domain =3D vdev->as.domain; + struct vduse_iova_domain *domain; + size_t bounce_size; =20 - return dma_addr < domain->bounce_size; + read_lock(&vdev->domain_lock); + domain =3D token.group->domain; + bounce_size =3D domain->bounce_size; + read_unlock(&vdev->domain_lock); + + return dma_addr < bounce_size; } =20 static int vduse_dev_mapping_error(union virtio_map token, dma_addr_t dma_= addr) @@ -933,9 +1005,15 @@ static int vduse_dev_mapping_error(union virtio_map t= oken, dma_addr_t dma_addr) static size_t vduse_dev_max_mapping_size(union virtio_map token) { struct vduse_dev *vdev =3D token.group->dev; - struct vduse_iova_domain *domain =3D vdev->as.domain; + struct vduse_iova_domain *domain; + size_t bounce_size; + + read_lock(&vdev->domain_lock); + domain =3D token.group->domain; + bounce_size =3D domain->bounce_size; + read_unlock(&vdev->domain_lock); =20 - return domain->bounce_size; + return bounce_size; } =20 static const struct virtio_map_ops vduse_map_ops =3D { @@ -1075,39 +1153,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->as.mem_lock); + mutex_lock(&dev->as[asid].mem_lock); ret =3D -ENOENT; - if (!dev->as.umem) + if (!dev->as[asid].umem) goto unlock; =20 ret =3D -EINVAL; - if (!dev->as.domain) + if (!dev->as[asid].domain) goto unlock; =20 - if (dev->as.umem->iova !=3D iova || size !=3D dev->as.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->as.domain); - unpin_user_pages_dirty_lock(dev->as.umem->pages, - dev->as.umem->npages, true); - atomic64_sub(dev->as.umem->npages, &dev->as.umem->mm->pinned_vm); - mmdrop(dev->as.umem->mm); - vfree(dev->as.umem->pages); - kfree(dev->as.umem); - dev->as.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->as.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; @@ -1115,14 +1194,14 @@ static int vduse_dev_reg_umem(struct vduse_dev *dev, unsigned long npages, lock_limit; int ret; =20 - if (!dev->as.domain || !dev->as.domain->bounce_map || - size !=3D dev->as.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->as.mem_lock); + mutex_lock(&dev->as[asid].mem_lock); ret =3D -EEXIST; - if (dev->as.umem) + if (dev->as[asid].umem) goto unlock; =20 ret =3D -ENOMEM; @@ -1146,7 +1225,7 @@ static int vduse_dev_reg_umem(struct vduse_dev *dev, goto out; } =20 - ret =3D vduse_domain_add_user_bounce_pages(dev->as.domain, + ret =3D vduse_domain_add_user_bounce_pages(dev->as[asid].domain, page_list, pinned); if (ret) goto out; @@ -1159,7 +1238,7 @@ static int vduse_dev_reg_umem(struct vduse_dev *dev, umem->mm =3D current->mm; mmgrab(current->mm); =20 - dev->as.umem =3D umem; + dev->as[asid].umem =3D umem; out: if (ret && pinned > 0) unpin_user_pages(page_list, pinned); @@ -1170,7 +1249,7 @@ static int vduse_dev_reg_umem(struct vduse_dev *dev, vfree(page_list); kfree(umem); } - mutex_unlock(&dev->as.mem_lock); + mutex_unlock(&dev->as[asid].mem_lock); return ret; } =20 @@ -1202,47 +1281,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 read_lock(&dev->domain_lock); - if (!dev->as.domain) { + asid =3D array_index_nospec(entry.asid, dev->nas); + if (!dev->as[asid].domain) { read_unlock(&dev->domain_lock); break; } - spin_lock(&dev->as.domain->iotlb_lock); - map =3D vhost_iotlb_itree_first(dev->as.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->as.domain->iotlb_lock); + spin_unlock(&dev->as[asid].domain->iotlb_lock); read_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) { 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; } @@ -1384,6 +1482,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))) @@ -1391,17 +1490,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 write_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); write_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))) @@ -1409,10 +1512,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; + write_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); write_unlock(&dev->domain_lock); break; @@ -1420,6 +1528,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))) @@ -1433,23 +1542,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; + read_lock(&dev->domain_lock); - if (!dev->as.domain) { + asid =3D array_index_nospec(info.asid, dev->nas); + if (!dev->as[asid].domain) { read_unlock(&dev->domain_lock); break; } - spin_lock(&dev->as.domain->iotlb_lock); - map =3D vhost_iotlb_itree_first(dev->as.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->as.domain->bounce_map && map->start =3D=3D 0 && - map->last =3D=3D dev->as.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->as.domain->iotlb_lock); + spin_unlock(&dev->as[asid].domain->iotlb_lock); read_unlock(&dev->domain_lock); if (!map) break; @@ -1474,8 +1591,10 @@ static int vduse_dev_release(struct inode *inode, st= ruct file *file) struct vduse_dev *dev =3D file->private_data; =20 write_lock(&dev->domain_lock); - if (dev->as.domain) - vduse_dev_dereg_umem(dev, 0, dev->as.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); write_unlock(&dev->domain_lock); spin_lock(&dev->msg_lock); /* Make sure the inflight messages can processed after reconncection */ @@ -1694,7 +1813,6 @@ static struct vduse_dev *vduse_dev_create(void) return NULL; =20 mutex_init(&dev->lock); - mutex_init(&dev->as.mem_lock); rwlock_init(&dev->domain_lock); spin_lock_init(&dev->msg_lock); INIT_LIST_HEAD(&dev->send_list); @@ -1745,8 +1863,11 @@ static int vduse_destroy_dev(char *name) idr_remove(&vduse_idr, dev->minor); kvfree(dev->config); vduse_dev_deinit_vqs(dev); - if (dev->as.domain) - vduse_domain_destroy(dev->as.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); @@ -1793,12 +1914,16 @@ 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 > 0xffff) return false; =20 + if (api_version >=3D VDUSE_API_VERSION_1 && config->nas > 0xffff) + return false; + if (config->vq_align > PAGE_SIZE) return false; =20 @@ -1862,7 +1987,8 @@ static ssize_t bounce_size_store(struct device *devic= e, =20 ret =3D -EPERM; write_lock(&dev->domain_lock); - if (dev->as.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); @@ -1923,6 +2049,13 @@ static int vduse_create_dev(struct vduse_dev_config = *config, for (u32 i =3D 0; i < dev->ngroups; ++i) dev->groups[i].dev =3D dev; =20 + dev->nas =3D (dev->api_version < 1) ? 1 : (config->nas ?: 1); + 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); + dev->name =3D kstrdup(config->name, GFP_KERNEL); if (!dev->name) goto err_str; @@ -1959,6 +2092,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); @@ -2084,7 +2219,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 @@ -2113,11 +2248,20 @@ static int vdpa_dev_add(struct vdpa_mgmt_dev *mdev,= const char *name, return ret; =20 write_lock(&dev->domain_lock); - if (!dev->as.domain) - dev->as.domain =3D vduse_domain_create(VDUSE_IOVA_SIZE - 1, - dev->bounce_size); + ret =3D 0; + + for (int i =3D 0; i < dev->nas; ++i) { + dev->as[i].domain =3D vduse_domain_create(VDUSE_IOVA_SIZE - 1, + dev->bounce_size); + if (!dev->as[i].domain) { + ret =3D -ENOMEM; + for (int j =3D 0; j < i; ++j) + vduse_domain_destroy(dev->as[j].domain); + } + } + write_unlock(&dev->domain_lock); - if (!dev->as.domain) { + if (ret =3D=3D -ENOMEM) { put_device(&dev->vdev->vdpa.dev); return -ENOMEM; } @@ -2126,8 +2270,12 @@ static int vdpa_dev_add(struct vdpa_mgmt_dev *mdev, = const char *name, if (ret) { put_device(&dev->vdev->vdpa.dev); write_lock(&dev->domain_lock); - vduse_domain_destroy(dev->as.domain); - dev->as.domain =3D NULL; + for (int i =3D 0; i < dev->nas; i++) { + if (dev->as[i].domain) { + vduse_domain_destroy(dev->as[i].domain); + dev->as[i].domain =3D NULL; + } + } write_unlock(&dev->domain_lock); return ret; } diff --git a/include/uapi/linux/vduse.h b/include/uapi/linux/vduse.h index a3d51cf6df3a..da73c3f2c280 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 - virtqueue group + @ @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,9 @@ struct vduse_dev_request { struct vduse_vq_state vq_state; struct vduse_dev_status s; struct vduse_iova_range iova; + /* Following members 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.51.0 From nobody Thu Oct 2 13:01:36 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 C668631D728 for ; Tue, 16 Sep 2025 13:09:20 +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=1758028162; cv=none; b=Jpy9R7uMqJD5/amymPwYmQmISM0/us2xSt02EXyxwBSpdZlpMb+blavi6MVmjjs1+I9wbK6zL9N2Einsf6Xq3knQDZPE60+09kbm0ADktWS9ict0fH5ZIo/V5e6RunlA71OIVdQknRF6nw9Q7RREm3fAjioraYhljkxMapKBWdo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758028162; c=relaxed/simple; bh=yNbddk/LPvzfkPrV9y5hG4H42GsSxK1SYVRooxnUSoY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=TeXSl1QgK28GcvyOpCDdsWXJ4HHknjd33xc190NuGChab4MKRFMMkcvZNrvVMDp3IaOIGLZm90mmhtSsezxsfJ7R0Curu7wLlWDjif9yPQZKBnUcodxe6L95XSXG+37bFm/QyLoQBqbMEpVCBu2lm8X2s0DJXzXvRWG01qNrTrI= 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=WI4e49J2; 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="WI4e49J2" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1758028159; 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=Mpep6nqvwYnlF3jq6jfNozm2GQkJFdMgDRzXN6d9G1A=; b=WI4e49J2gRByKoRegiGI4brsgLwKW1WdyqfUVbp1BTvnNvbf8cvW4QD2/s8E2LGulXqh7H lPfcM52qOCM22Y2/m/D61jkEhHBH6xdEIqMZb3lE1YyMCFYkNGbcsbSa2WRG3pLKlo6pMr qrcSJN2cIR//pLoKSSGq901OB1lqdyQ= Received: from mx-prod-mc-02.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-683-Ek6LlDM0NKKm9vVScxbUjg-1; Tue, 16 Sep 2025 09:09:18 -0400 X-MC-Unique: Ek6LlDM0NKKm9vVScxbUjg-1 X-Mimecast-MFC-AGG-ID: Ek6LlDM0NKKm9vVScxbUjg_1758028156 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id C45DB195608A; Tue, 16 Sep 2025 13:09:16 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.44.34.12]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 980F519560B8; Tue, 16 Sep 2025 13:09:12 +0000 (UTC) From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= To: "Michael S . Tsirkin " Cc: Stefano Garzarella , jasowang@redhat.com, Xuan Zhuo , linux-kernel@vger.kernel.org, Maxime Coquelin , Yongji Xie , Cindy Lu , Laurent Vivier , virtualization@lists.linux.dev, =?UTF-8?q?Eugenio=20P=C3=A9rez?= Subject: [PATCH v2 7/7] vduse: bump version number Date: Tue, 16 Sep 2025 15:08:33 +0200 Message-ID: <20250916130833.329087-8-eperezma@redhat.com> In-Reply-To: <20250916130833.329087-1-eperezma@redhat.com> References: <20250916130833.329087-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.0 on 10.30.177.12 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. 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 06b7790380b7..07ef309ed7f7 100644 --- a/drivers/vdpa/vdpa_user/vduse_dev.c +++ b/drivers/vdpa/vdpa_user/vduse_dev.c @@ -2121,7 +2121,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; @@ -2188,7 +2188,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.51.0