From nobody Wed Dec 31 11:09:43 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AAD3EC4332F for ; Fri, 3 Nov 2023 17:17:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230232AbjKCRRk (ORCPT ); Fri, 3 Nov 2023 13:17:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51606 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229482AbjKCRRj (ORCPT ); Fri, 3 Nov 2023 13:17:39 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A7F1CD48 for ; Fri, 3 Nov 2023 10:16:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699031815; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=OZOkp7RIIlEcdzCc+o8G685sXVkqf9owoeJPzWLLPLs=; b=DXAglskqoBjX1zAit+HiPrFFFIgik4spl9dgitQ0t1GFh/BYuLCJYEmlbtN3XOkAyFhcOv Z0KHobG2TLP4uJn1XStEOZCduueCjCfcejL1tMvNg/eavO6LA06tl5l1b6xJ1Q3NX26myw 52l83kD5zM8xotg0oiCfj4DHxaXdsCI= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-589-s4ZotfSOOLO-hga7WjSckw-1; Fri, 03 Nov 2023 13:16:52 -0400 X-MC-Unique: s4ZotfSOOLO-hga7WjSckw-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id 21CC1811E88; Fri, 3 Nov 2023 17:16:52 +0000 (UTC) Received: from server.redhat.com (unknown [10.72.112.41]) by smtp.corp.redhat.com (Postfix) with ESMTP id F145740C6ECE; Fri, 3 Nov 2023 17:16:48 +0000 (UTC) From: Cindy Lu To: lulu@redhat.com, jasowang@redhat.com, mst@redhat.com, yi.l.liu@intel.com, jgg@nvidia.com, linux-kernel@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org Subject: [RFC v1 1/8] vhost/iommufd: Add the functions support iommufd Date: Sat, 4 Nov 2023 01:16:34 +0800 Message-Id: <20231103171641.1703146-2-lulu@redhat.com> In-Reply-To: <20231103171641.1703146-1-lulu@redhat.com> References: <20231103171641.1703146-1-lulu@redhat.com> 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.11.54.2 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add a new file vhost/iommufd.c to support the function of iommufd, This file contains iommufd function=C2=A0of emulated device and the physical device.=C2=A0 Signed-off-by: Cindy Lu --- drivers/vhost/iommufd.c | 178 ++++++++++++++++++++++++++++++++++++++++ drivers/vhost/vhost.h | 21 +++++ 2 files changed, 199 insertions(+) create mode 100644 drivers/vhost/iommufd.c diff --git a/drivers/vhost/iommufd.c b/drivers/vhost/iommufd.c new file mode 100644 index 000000000000..113dda50a9b6 --- /dev/null +++ b/drivers/vhost/iommufd.c @@ -0,0 +1,178 @@ +#include +#include + +#include "vhost.h" + +MODULE_IMPORT_NS(IOMMUFD); + +int vdpa_iommufd_bind(struct vdpa_device *vdpa, struct iommufd_ctx *ictx, + u32 *ioas_id, u32 *device_id) +{ + int ret; + + vhost_vdpa_lockdep_assert_held(vdpa); + + /* + * If the driver doesn't provide this op then it means the device d= oes + * not do DMA at all. So nothing to do. + */ + if (!vdpa->config->bind_iommufd) + return 0; + ret =3D vdpa->config->bind_iommufd(vdpa, ictx, device_id); + if (ret) + return ret; + + return 0; +} + +void vdpa_iommufd_unbind(struct vdpa_device *vdpa) +{ + vhost_vdpa_lockdep_assert_held(vdpa); + + if (vdpa->config->unbind_iommufd) + vdpa->config->unbind_iommufd(vdpa); +} + +int vdpa_iommufd_physical_bind(struct vdpa_device *vdpa, + struct iommufd_ctx *ictx, u32 *out_device_id) +{ + struct device *dma_dev =3D vdpa_get_dma_dev(vdpa); + struct iommufd_device *idev; + + idev =3D iommufd_device_bind(ictx, dma_dev, out_device_id); + if (IS_ERR(idev)) + return PTR_ERR(idev); + vdpa->iommufd_device =3D idev; + return 0; +} +EXPORT_SYMBOL_GPL(vdpa_iommufd_physical_bind); + +void vdpa_iommufd_physical_unbind(struct vdpa_device *vdpa) +{ + vhost_vdpa_lockdep_assert_held(vdpa); + + if (vdpa->iommufd_attached) { + iommufd_device_detach(vdpa->iommufd_device); + vdpa->iommufd_attached =3D false; + } + iommufd_device_unbind(vdpa->iommufd_device); + vdpa->iommufd_device =3D NULL; +} +EXPORT_SYMBOL_GPL(vdpa_iommufd_physical_unbind); + +int vdpa_iommufd_physical_attach_ioas(struct vdpa_device *vdpa, + u32 *iommufd_ioasid) +{ + int rc; + + vhost_vdpa_lockdep_assert_held(vdpa); + + if (WARN_ON(!vdpa->iommufd_device)) + return -EINVAL; + + if (vdpa->iommufd_attached) + rc =3D iommufd_device_replace(vdpa->iommufd_device, + iommufd_ioasid); + else + rc =3D iommufd_device_attach(vdpa->iommufd_device, + iommufd_ioasid); + if (rc) + return rc; + vdpa->iommufd_attached =3D true; + + return 0; +} + +EXPORT_SYMBOL_GPL(vdpa_iommufd_physical_attach_ioas); +int vdpa_iommufd_physical_detach_ioas(struct vdpa_device *vdpa) +{ + vhost_vdpa_lockdep_assert_held(vdpa); + + if (WARN_ON(!vdpa->iommufd_device) || !vdpa->iommufd_attached) + return -1; + + iommufd_device_detach(vdpa->iommufd_device); + vdpa->iommufd_attached =3D false; + return 0; +} +EXPORT_SYMBOL_GPL(vdpa_iommufd_physical_detach_ioas); + +static void vdpa_emulated_unmap(void *data, unsigned long iova, + unsigned long length) +{ + struct vdpa_device *vdpa =3D data; + /* todo: need to unmap the iova-lenth in all ASID*/ + + // vdpa->config->dma_unmap(vdpa, 0, iova, length); +} + +static const struct iommufd_access_ops vdpa_user_ops =3D { + .needs_pin_pages =3D 1, + .unmap =3D vdpa_emulated_unmap, +}; + +int vdpa_iommufd_emulated_bind(struct vdpa_device *vdpa, + struct iommufd_ctx *ictx, u32 *out_device_id) +{ + vhost_vdpa_lockdep_assert_held(vdpa); + + struct iommufd_access *user; + + user =3D iommufd_access_create(ictx, &vdpa_user_ops, vdpa, out_device_id); + if (IS_ERR(user)) + return PTR_ERR(user); + vdpa->iommufd_access =3D user; + return 0; +} +EXPORT_SYMBOL_GPL(vdpa_iommufd_emulated_bind); + +void vdpa_iommufd_emulated_unbind(struct vdpa_device *vdpa) +{ + vhost_vdpa_lockdep_assert_held(vdpa); + + if (vdpa->iommufd_access) { + iommufd_access_destroy(vdpa->iommufd_access); + vdpa->iommufd_attached =3D false; + vdpa->iommufd_access =3D NULL; + } +} +EXPORT_SYMBOL_GPL(vdpa_iommufd_emulated_unbind); + +int vdpa_iommufd_emulated_attach_ioas(struct vdpa_device *vdpa, + u32 *iommufd_ioasid) +{ + int rc; + + struct iommufd_access *user; + + vhost_vdpa_lockdep_assert_held(vdpa); + + if (vdpa->iommufd_attached) { + rc =3D iommufd_access_replace(vdpa->iommufd_access, + *iommufd_ioasid); + } else { + rc =3D iommufd_access_attach(vdpa->iommufd_access, + *iommufd_ioasid); + } + user =3D vdpa->iommufd_access; + + if (rc) + return rc; + vdpa->iommufd_attached =3D true; + return 0; +} +EXPORT_SYMBOL_GPL(vdpa_iommufd_emulated_attach_ioas); + +int vdpa_iommufd_emulated_detach_ioas(struct vdpa_device *vdpa) +{ + vhost_vdpa_lockdep_assert_held(vdpa); + + if (WARN_ON(!vdpa->iommufd_access) || !vdpa->iommufd_attached) + return -1; + + iommufd_access_detach(vdpa->iommufd_access); + vdpa->iommufd_attached =3D false; + + return 0; +} +EXPORT_SYMBOL_GPL(vdpa_iommufd_emulated_detach_ioas); diff --git a/drivers/vhost/vhost.h b/drivers/vhost/vhost.h index f60d5f7bef94..179012e350f9 100644 --- a/drivers/vhost/vhost.h +++ b/drivers/vhost/vhost.h @@ -310,6 +310,27 @@ static inline bool vhost_is_little_endian(struct vhost= _virtqueue *vq) } #endif =20 +struct iommufd_ctx; +struct vdpa_device; +void vhost_vdpa_lockdep_assert_held(struct vdpa_device *vdpa); + +#if IS_ENABLED(CONFIG_IOMMUFD) +int vdpa_iommufd_bind(struct vdpa_device *vdpa, struct iommufd_ctx *ictx, + u32 *ioas_id, u32 *device_id); +void vdpa_iommufd_unbind(struct vdpa_device *vdpa); +#else +static inline int vdpa_iommufd_bind(struct vdpa_device *vdpa, + struct iommufd_ctx *ictx, u32 *ioas_id, + u32 *device_id) +{ + return -EOPNOTSUPP; +} + +static inline void vdpa_iommufd_unbind(struct vdpa_device *vdpa) +{ +} +#endif + /* Memory accessors */ static inline u16 vhost16_to_cpu(struct vhost_virtqueue *vq, __virtio16 va= l) { --=20 2.34.3 From nobody Wed Dec 31 11:09:43 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3EF19C4167B for ; Fri, 3 Nov 2023 17:17:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345870AbjKCRRt (ORCPT ); Fri, 3 Nov 2023 13:17:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51644 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229482AbjKCRRo (ORCPT ); Fri, 3 Nov 2023 13:17:44 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 652181BD for ; Fri, 3 Nov 2023 10:16:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699031817; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=T2BDwvqkQrafYreOuDJL/fbzR9dfSYQpbXhoeoCg4t4=; b=KZICXSPfKJFv1Pr8iADOaCuMqzcbUyxqlWevAIbjyOdwMseC5Vxqw6vuOlKPYnTWjfVviL KlOrrXHTenTngTu1okD8luEXknMV9eRBnglCPFWt+RMrCUPhE0HSxHSq79o98zDHzB0+Up SLCZsCPypcrieu0k56X3+AkjcEGoPN4= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-605-pleYeuzlM6i_Grq8vQ4w-g-1; Fri, 03 Nov 2023 13:16:56 -0400 X-MC-Unique: pleYeuzlM6i_Grq8vQ4w-g-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id D79E985A58A; Fri, 3 Nov 2023 17:16:55 +0000 (UTC) Received: from server.redhat.com (unknown [10.72.112.41]) by smtp.corp.redhat.com (Postfix) with ESMTP id B65AE40C6EBC; Fri, 3 Nov 2023 17:16:52 +0000 (UTC) From: Cindy Lu To: lulu@redhat.com, jasowang@redhat.com, mst@redhat.com, yi.l.liu@intel.com, jgg@nvidia.com, linux-kernel@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org Subject: [RFC v1 2/8] Kconfig: Add the new file vhost/iommufd Date: Sat, 4 Nov 2023 01:16:35 +0800 Message-Id: <20231103171641.1703146-3-lulu@redhat.com> In-Reply-To: <20231103171641.1703146-1-lulu@redhat.com> References: <20231103171641.1703146-1-lulu@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Change the makefile and Kconfig, to add the new file vhost/iommufd.c Signed-off-by: Cindy Lu --- drivers/vhost/Kconfig | 1 + drivers/vhost/Makefile | 1 + 2 files changed, 2 insertions(+) diff --git a/drivers/vhost/Kconfig b/drivers/vhost/Kconfig index b455d9ab6f3d..a4becfb36d77 100644 --- a/drivers/vhost/Kconfig +++ b/drivers/vhost/Kconfig @@ -72,6 +72,7 @@ config VHOST_VDPA select VHOST select IRQ_BYPASS_MANAGER depends on VDPA + depends on IOMMUFD || !IOMMUFD help This kernel module can be loaded in host kernel to accelerate guest virtio devices with the vDPA-based backends. diff --git a/drivers/vhost/Makefile b/drivers/vhost/Makefile index f3e1897cce85..cda7f6b7f8da 100644 --- a/drivers/vhost/Makefile +++ b/drivers/vhost/Makefile @@ -12,6 +12,7 @@ obj-$(CONFIG_VHOST_RING) +=3D vringh.o =20 obj-$(CONFIG_VHOST_VDPA) +=3D vhost_vdpa.o vhost_vdpa-y :=3D vdpa.o +vhost_vdpa-$(CONFIG_IOMMUFD) +=3D iommufd.o =20 obj-$(CONFIG_VHOST) +=3D vhost.o =20 --=20 2.34.3 From nobody Wed Dec 31 11:09:43 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4B902C4332F for ; Fri, 3 Nov 2023 17:17:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345872AbjKCRRy (ORCPT ); Fri, 3 Nov 2023 13:17:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36840 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234471AbjKCRRv (ORCPT ); Fri, 3 Nov 2023 13:17:51 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B163CD47 for ; Fri, 3 Nov 2023 10:17:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699031823; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=JjCcpTQrJmsF+m1LiuhY/lfZrSnOFnb7RQ3HwNEy8v8=; b=ENK+mJ1ndLrunPqVcDm5SAIrDtynqIliDPt/gFL8pYltHCnMvtXi06MJMnLa97lWXS+JZo 8hllChZtpqayo3Fjy1FIEWXbaIjaasYohWC9fb19mG3CUEwcNTFmzWi0b1CzHPysqjfATe B0m4zOWOMDDG4hEJptS0NAEuBK18s+U= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-61-Lrm-ZPJtNbC55VC5JqJ0OA-1; Fri, 03 Nov 2023 13:17:00 -0400 X-MC-Unique: Lrm-ZPJtNbC55VC5JqJ0OA-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id 99F131C29AE0; Fri, 3 Nov 2023 17:16:59 +0000 (UTC) Received: from server.redhat.com (unknown [10.72.112.41]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7919440C6EBF; Fri, 3 Nov 2023 17:16:56 +0000 (UTC) From: Cindy Lu To: lulu@redhat.com, jasowang@redhat.com, mst@redhat.com, yi.l.liu@intel.com, jgg@nvidia.com, linux-kernel@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org Subject: [RFC v1 3/8] vhost: Add 3 new uapi to support iommufd Date: Sat, 4 Nov 2023 01:16:36 +0800 Message-Id: <20231103171641.1703146-4-lulu@redhat.com> In-Reply-To: <20231103171641.1703146-1-lulu@redhat.com> References: <20231103171641.1703146-1-lulu@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" VHOST_VDPA_SET_IOMMU_FD: bind the device to iommufd device VDPA_DEVICE_ATTACH_IOMMUFD_AS: Attach a vdpa device to an iommufd address space specified by IOAS id. VDPA_DEVICE_DETACH_IOMMUFD_AS: Detach a vdpa device from the iommufd address space Signed-off-by: Cindy Lu --- drivers/vhost/vdpa.c | 171 +++++++++++++++++++++++++++++++++++++ include/uapi/linux/vhost.h | 66 ++++++++++++++ 2 files changed, 237 insertions(+) diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c index 78379ffd2336..dfaddd833364 100644 --- a/drivers/vhost/vdpa.c +++ b/drivers/vhost/vdpa.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -25,6 +26,8 @@ =20 #include "vhost.h" =20 +MODULE_IMPORT_NS(IOMMUFD); + enum { VHOST_VDPA_BACKEND_FEATURES =3D (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2) | @@ -69,6 +72,15 @@ static void vhost_vdpa_iotlb_unmap(struct vhost_vdpa *v, struct vhost_iotlb *iotlb, u64 start, u64 last, u32 asid); =20 +void vhost_vdpa_lockdep_assert_held(struct vdpa_device *vdpa) +{ + struct vhost_vdpa *v =3D vdpa_get_drvdata(vdpa); + + if (WARN_ON(!v)) + return; + lockdep_assert_held(&v->vdev.mutex); +} + static inline u32 iotlb_to_asid(struct vhost_iotlb *iotlb) { struct vhost_vdpa_as *as =3D container_of(iotlb, struct @@ -551,6 +563,149 @@ static long vhost_vdpa_suspend(struct vhost_vdpa *v) =20 return ops->suspend(vdpa); } +static long vhost_vdpa_iommufd_set_device(struct vhost_vdpa *v, + void __user *argp) +{ + struct device *dma_dev =3D vdpa_get_dma_dev(v->vdpa); + struct vhost_vdpa_set_iommufd set_iommufd; + struct vdpa_device *vdpa =3D v->vdpa; + struct iommufd_ctx *ictx; + unsigned long minsz; + u32 ioas_id, dev_id; + struct fd f; + long r =3D 0; + + minsz =3D offsetofend(struct vhost_vdpa_set_iommufd, iommufd_ioasid); + if (copy_from_user(&set_iommufd, argp, minsz)) + return -EFAULT; + + /* Unset IOMMUFD */ + if (set_iommufd.iommufd < 0) { + if (!vdpa->iommufd_ictx || !vdpa->iommufd_device) + return -EINVAL; + if (atomic_read(&vdpa->iommufd_users)) { + atomic_dec(&vdpa->iommufd_users); + return 0; + } + vdpa_iommufd_unbind(v->vdpa); + vdpa->iommufd_device =3D NULL; + vdpa->iommufd_ictx =3D NULL; + return iommu_attach_device(v->domain, dma_dev); + } + + /* For same device but different groups, ++refcount only */ + if (vdpa->iommufd_device) + goto out_inc; + + r =3D -EBADF; + f =3D fdget(set_iommufd.iommufd); + if (!f.file) + goto out; + + r =3D -EINVAL; + ictx =3D iommufd_ctx_from_file(f.file); + if (IS_ERR(ictx)) + goto out_fdput; + + if (v->domain) { + iommu_device_unuse_default_domain(dma_dev); + iommu_detach_device(v->domain, dma_dev); + } + + ioas_id =3D set_iommufd.iommufd_ioasid; + r =3D vdpa_iommufd_bind(vdpa, ictx, &ioas_id, &dev_id); + if (r) + goto out_reattach; + + set_iommufd.out_dev_id =3D dev_id; + r =3D copy_to_user(argp + minsz, &set_iommufd.out_dev_id, + sizeof(set_iommufd.out_dev_id)) ? + -EFAULT : + 0; + if (r) + goto out_device_unbind; + + vdpa->iommufd_ictx =3D ictx; + +out_inc: + atomic_inc(&vdpa->iommufd_users); + + goto out_fdput; + +out_device_unbind: + + vdpa_iommufd_unbind(vdpa); +out_reattach: + iommu_device_use_default_domain(dma_dev); + iommu_attach_device(v->domain, dma_dev); + iommufd_ctx_put(ictx); +out_fdput: + fdput(f); +out: + return r; +} +int vhost_vdpa_iommufd_ioas_attach(struct vhost_vdpa *v, void __user *arg) +{ + struct vdpa_device_attach_iommufd_as attach; + unsigned long minsz; + int ret; + + minsz =3D offsetofend(struct vdpa_device_attach_iommufd_as, ioas_id); + + if (copy_from_user(&attach, (void __user *)arg, minsz)) + return -EFAULT; + + if (attach.argsz < minsz || attach.flags) + return -EINVAL; + + if (!v->vdpa->config->bind_iommufd) + return -ENODEV; + + if (!v->vdpa->iommufd_ictx) { + ret =3D -EINVAL; + return ret; + } + + ret =3D v->vdpa->config->attach_ioas(v->vdpa, &attach.ioas_id); + + if (ret) + return ret; + + ret =3D copy_to_user( + (void __user *)arg + + offsetofend(struct vdpa_device_attach_iommufd_as, + flags), + &attach.ioas_id, sizeof(attach.ioas_id)) ? + -EFAULT : + 0; + + if (ret) + return ret; + + return 0; +} + +int vhost_vdpa_iommufd_ioas_detach(struct vhost_vdpa *v, void __user *arg) +{ + struct vdpa_device_detach_iommufd_as detach; + unsigned long minsz; + + minsz =3D offsetofend(struct vdpa_device_detach_iommufd_as, flags); + + if (copy_from_user(&detach, (void __user *)arg, minsz)) + return -EFAULT; + + if (detach.argsz < minsz || detach.flags) + return -EINVAL; + + if (!v->vdpa->config->bind_iommufd) + return -ENODEV; + + if (v->vdpa->iommufd_ictx) { + return -EINVAL; + } + return v->vdpa->config->detach_ioas(v->vdpa); +} =20 /* After a successful return of this ioctl the device resumes processing * virtqueue descriptors. The device becomes fully operational the same wa= y it @@ -744,6 +899,18 @@ static long vhost_vdpa_unlocked_ioctl(struct file *fil= ep, case VHOST_SET_LOG_FD: r =3D -ENOIOCTLCMD; break; + case VHOST_VDPA_SET_IOMMU_FD: + + r =3D vhost_vdpa_iommufd_set_device(v, argp); + break; + case VDPA_DEVICE_ATTACH_IOMMUFD_AS: + r =3D vhost_vdpa_iommufd_ioas_attach(v, (void __user *)arg); + break; + + case VDPA_DEVICE_DETACH_IOMMUFD_AS: + r =3D vhost_vdpa_iommufd_ioas_detach(v, (void __user *)arg); + break; + case VHOST_VDPA_SET_CONFIG_CALL: r =3D vhost_vdpa_set_config_call(v, argp); break; @@ -896,6 +1063,10 @@ static int vhost_vdpa_map(struct vhost_vdpa *v, struc= t vhost_iotlb *iotlb, } else if (ops->set_map) { if (!v->in_batch) r =3D ops->set_map(vdpa, asid, iotlb); + } else if (!vdpa->iommufd_ictx) { + /* Legacy iommu domain pathway without IOMMUFD */ + r =3D iommu_map(v->domain, iova, pa, size, + perm_to_iommu_flags(perm), GFP_KERNEL); } else { r =3D iommu_map(v->domain, iova, pa, size, perm_to_iommu_flags(perm), GFP_KERNEL); diff --git a/include/uapi/linux/vhost.h b/include/uapi/linux/vhost.h index f5c48b61ab62..07e1b2c443ca 100644 --- a/include/uapi/linux/vhost.h +++ b/include/uapi/linux/vhost.h @@ -219,4 +219,70 @@ */ #define VHOST_VDPA_RESUME _IO(VHOST_VIRTIO, 0x7E) =20 +/* vhost_vdpa_set_iommufd + * Input parameters: + * @iommufd: file descriptor from /dev/iommu; pass -1 to unset + * @iommufd_ioasid: IOAS identifier returned from ioctl(IOMMU_IOAS_ALLOC) + * Output parameters: + * @out_dev_id: device identifier + */ +struct vhost_vdpa_set_iommufd { + __s32 iommufd; + __u32 iommufd_ioasid; + __u32 out_dev_id; +}; + +#define VHOST_VDPA_SET_IOMMU_FD \ + _IOW(VHOST_VIRTIO, 0x7F, struct vhost_vdpa_set_iommufd) + +/* + * VDPA_DEVICE_ATTACH_IOMMUFD_AS - + * _IOW(VHOST_VIRTIO, 0x7f, struct vdpa_device_attach_iommufd_as) + * + * Attach a vdpa device to an iommufd address space specified by IOAS + * id. + * + * Available only after a device has been bound to iommufd via + * VHOST_VDPA_SET_IOMMU_FD + * + * Undo by VDPA_DEVICE_DETACH_IOMMUFD_AS or device fd close. + * + * @argsz: user filled size of this data. + * @flags: must be 0. + * @ioas_id: Input the target id which can represent an ioas + * allocated via iommufd subsystem. + * + * Return: 0 on success, -errno on failure. + */ +struct vdpa_device_attach_iommufd_as { + __u32 argsz; + __u32 flags; + __u32 ioas_id; +}; + +#define VDPA_DEVICE_ATTACH_IOMMUFD_AS \ + _IOW(VHOST_VIRTIO, 0x82, struct vdpa_device_attach_iommufd_as) + +/* + * VDPA_DEVICE_DETACH_IOMMUFD_AS + * + * Detach a vdpa device from the iommufd address space it has been + * attached to. After it, device should be in a blocking DMA state. + * + * Available only after a device has been bound to iommufd via + * VHOST_VDPA_SET_IOMMU_FD + * + * @argsz: user filled size of this data. + * @flags: must be 0. + * + * Return: 0 on success, -errno on failure. + */ +struct vdpa_device_detach_iommufd_as { + __u32 argsz; + __u32 flags; +}; + +#define VDPA_DEVICE_DETACH_IOMMUFD_AS \ + _IOW(VHOST_VIRTIO, 0x83, struct vdpa_device_detach_iommufd_as) + #endif --=20 2.34.3 From nobody Wed Dec 31 11:09:43 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7CF13C4332F for ; Fri, 3 Nov 2023 17:18:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377143AbjKCRSW (ORCPT ); Fri, 3 Nov 2023 13:18:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43392 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234418AbjKCRST (ORCPT ); Fri, 3 Nov 2023 13:18:19 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5DC29D4C for ; Fri, 3 Nov 2023 10:17:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699031852; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=xuqvr5RWRiWU1vecaSpLFCcLTpJ+4Yg7zkI2ypzhUqw=; b=LgDInuxjPlVIe8Dcj2bevueBD8tDWgdcM04qFir7d3zsXAl8Bow+IafEqWOLNLtw+NdjQI ODFXOKYsk78viujuiKOTX0UQqr+/ersBDtLpSpOmYvPyvKvLdygrY6cHZRE/FTy9Z/JLHN ZpgtsrDSqwKjVPrMlpwlJ62sYIYtVn8= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-19-9U5j9LuUPY-QMBSo0Y4PAQ-1; Fri, 03 Nov 2023 13:17:25 -0400 X-MC-Unique: 9U5j9LuUPY-QMBSo0Y4PAQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id E7CE285D536; Fri, 3 Nov 2023 17:17:24 +0000 (UTC) Received: from server.redhat.com (unknown [10.72.112.41]) by smtp.corp.redhat.com (Postfix) with ESMTP id C264EC1290F; Fri, 3 Nov 2023 17:17:21 +0000 (UTC) From: Cindy Lu To: lulu@redhat.com, jasowang@redhat.com, mst@redhat.com, yi.l.liu@intel.com, jgg@nvidia.com, linux-kernel@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org Subject: [RFC v1 4/8] vdpa: Add new vdpa_config_ops to support iommufd Date: Sat, 4 Nov 2023 01:16:37 +0800 Message-Id: <20231103171641.1703146-5-lulu@redhat.com> In-Reply-To: <20231103171641.1703146-1-lulu@redhat.com> References: <20231103171641.1703146-1-lulu@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add 4 new vdpa_config_ops function to support iommufd Signed-off-by: Cindy Lu --- include/linux/vdpa.h | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/include/linux/vdpa.h b/include/linux/vdpa.h index 0e652026b776..233d80f9d910 100644 --- a/include/linux/vdpa.h +++ b/include/linux/vdpa.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -97,6 +98,12 @@ struct vdpa_device { struct vdpa_mgmt_dev *mdev; unsigned int ngroups; unsigned int nas; + struct iommufd_access *iommufd_access; + struct iommufd_device *iommufd_device; + struct iommufd_ctx *iommufd_ictx; + unsigned long *vq_bitmap; + atomic_t iommufd_users; + bool iommufd_attached; }; =20 /** @@ -332,6 +339,17 @@ struct vdpa_map_file { * @vdev: vdpa device * @free: Free resources that belongs to vDPA (optional) * @vdev: vdpa device + * @bind_iommufd: use vdpa_iommufd_physical_bind for an IOMMU + * backed device. + * otherwise use vdpa_iommufd_emulated_bind + * @unbind_iommufd: use vdpa_iommufd_physical_unbind for an IOM= MU + * backed device. + * otherwise, use vdpa_iommufd_emulated_unbind + * @attach_ioas: use vdpa_iommufd_physical_attach_ioas for an + * IOMMU backed device. + * @detach_ioas: Opposite of attach_ioas + * @free: Free resources that belongs to vDPA (optional) + * @vdev: vdpa device */ struct vdpa_config_ops { /* Virtqueue ops */ @@ -402,6 +420,13 @@ struct vdpa_config_ops { =20 /* Free device resources */ void (*free)(struct vdpa_device *vdev); + /* IOMMUFD ops */ + int (*bind_iommufd)(struct vdpa_device *vdev, struct iommufd_ctx *ictx, + u32 *out_device_id); + void (*unbind_iommufd)(struct vdpa_device *vdev); + int (*attach_ioas)(struct vdpa_device *vdev, u32 *pt_id); + int (*detach_ioas)(struct vdpa_device *vdev); + }; =20 struct vdpa_device *__vdpa_alloc_device(struct device *parent, @@ -570,4 +595,15 @@ struct vdpa_mgmt_dev { int vdpa_mgmtdev_register(struct vdpa_mgmt_dev *mdev); void vdpa_mgmtdev_unregister(struct vdpa_mgmt_dev *mdev); =20 -#endif /* _LINUX_VDPA_H */ +int vdpa_iommufd_physical_bind(struct vdpa_device *vdpa, + struct iommufd_ctx *ictx, u32 *out_device_id); +void vdpa_iommufd_physical_unbind(struct vdpa_device *vdpa); +int vdpa_iommufd_physical_attach_ioas(struct vdpa_device *vdpa, u32 *pt_id= ); +int vdpa_iommufd_physical_detach_ioas(struct vdpa_device *vdpa); +int vdpa_iommufd_emulated_bind(struct vdpa_device *vdpa, + struct iommufd_ctx *ictx, u32 *out_device_id); +void vdpa_iommufd_emulated_unbind(struct vdpa_device *vdpa); +int vdpa_iommufd_emulated_attach_ioas(struct vdpa_device *vdpa, u32 *pt_id= ); +int vdpa_iommufd_emulated_detach_ioas(struct vdpa_device *vdpa); + +#endif --=20 2.34.3 From nobody Wed Dec 31 11:09:43 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4EF7BC4332F for ; Fri, 3 Nov 2023 17:18:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377590AbjKCRS0 (ORCPT ); Fri, 3 Nov 2023 13:18:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40166 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377249AbjKCRSX (ORCPT ); Fri, 3 Nov 2023 13:18:23 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 05517D48 for ; Fri, 3 Nov 2023 10:17:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699031859; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=qZcijrBkenJpJgKIh5nn77fb44ksEJ9Uwp/HPtluVaY=; b=FLhpaRJ43evcLk4oVC0416wghzrtz+4oiTFODPMtRG8iQfwyB7zPuQfxnyy6KYi+SSeWyI zbSz3P9jRVj1Iv5pMknBHfBdWwkudzpl1DDdNJW3zanzRTbPmCCLsgdrXVFORxp1v1Rl1P 4fuvJo33hpdky5V1MDBmAnLphGI+uhI= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-396-WVF19FtuOU6EB2xsKppuQQ-1; Fri, 03 Nov 2023 13:17:34 -0400 X-MC-Unique: WVF19FtuOU6EB2xsKppuQQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id B31B6101389C; Fri, 3 Nov 2023 17:17:28 +0000 (UTC) Received: from server.redhat.com (unknown [10.72.112.41]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8A0CFC15983; Fri, 3 Nov 2023 17:17:25 +0000 (UTC) From: Cindy Lu To: lulu@redhat.com, jasowang@redhat.com, mst@redhat.com, yi.l.liu@intel.com, jgg@nvidia.com, linux-kernel@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org Subject: [RFC v1 5/8] vdpa_sim :Add support for iommufd Date: Sat, 4 Nov 2023 01:16:38 +0800 Message-Id: <20231103171641.1703146-6-lulu@redhat.com> In-Reply-To: <20231103171641.1703146-1-lulu@redhat.com> References: <20231103171641.1703146-1-lulu@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add new vdpa_config_ops function to support iommufd Signed-off-by: Cindy Lu --- drivers/vdpa/vdpa_sim/vdpa_sim.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_= sim.c index 76d41058add9..9400ec32ec41 100644 --- a/drivers/vdpa/vdpa_sim/vdpa_sim.c +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c @@ -762,6 +762,10 @@ static const struct vdpa_config_ops vdpasim_config_ops= =3D { .bind_mm =3D vdpasim_bind_mm, .unbind_mm =3D vdpasim_unbind_mm, .free =3D vdpasim_free, + .bind_iommufd =3D vdpa_iommufd_emulated_bind, + .unbind_iommufd =3D vdpa_iommufd_emulated_unbind, + .attach_ioas =3D vdpa_iommufd_emulated_attach_ioas, + .detach_ioas =3D vdpa_iommufd_emulated_detach_ioas, }; =20 static const struct vdpa_config_ops vdpasim_batch_config_ops =3D { @@ -799,6 +803,10 @@ static const struct vdpa_config_ops vdpasim_batch_conf= ig_ops =3D { .bind_mm =3D vdpasim_bind_mm, .unbind_mm =3D vdpasim_unbind_mm, .free =3D vdpasim_free, + .bind_iommufd =3D vdpa_iommufd_emulated_bind, + .unbind_iommufd =3D vdpa_iommufd_emulated_unbind, + .attach_ioas =3D vdpa_iommufd_emulated_attach_ioas, + .detach_ioas =3D vdpa_iommufd_emulated_detach_ioas, }; =20 MODULE_VERSION(DRV_VERSION); --=20 2.34.3 From nobody Wed Dec 31 11:09:43 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 07642C4332F for ; Fri, 3 Nov 2023 17:18:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377373AbjKCRSi (ORCPT ); Fri, 3 Nov 2023 13:18:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54258 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234418AbjKCRSg (ORCPT ); Fri, 3 Nov 2023 13:18:36 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6E821D59 for ; Fri, 3 Nov 2023 10:17:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699031863; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=GpK/Y1O55XislOg5N5jgXt0h6vbPef/n6csAVM8Xygs=; b=WW9A36V0M78YifZLp9JBpTkKsQDrxnlkCKOLmT0jEwNLcXhltvXPwOBi1ImKk/5xN1R36A ZY66fjG57TFhb5YyXerufluLtBPUfNDt1uVdzri9auPhUL+r4Q09avfn4O/c483R3zROGe Ymgk68YZjrhVH0WhNyTactvLsm6cqFI= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-91-44JTi4YMOiuZp0J0ge-3XA-1; Fri, 03 Nov 2023 13:17:37 -0400 X-MC-Unique: 44JTi4YMOiuZp0J0ge-3XA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id 775B33826D52; Fri, 3 Nov 2023 17:17:32 +0000 (UTC) Received: from server.redhat.com (unknown [10.72.112.41]) by smtp.corp.redhat.com (Postfix) with ESMTP id 54708C15983; Fri, 3 Nov 2023 17:17:29 +0000 (UTC) From: Cindy Lu To: lulu@redhat.com, jasowang@redhat.com, mst@redhat.com, yi.l.liu@intel.com, jgg@nvidia.com, linux-kernel@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org Subject: [RFC v1 6/8] vdpa: change the map/unmap process to support iommufd Date: Sat, 4 Nov 2023 01:16:39 +0800 Message-Id: <20231103171641.1703146-7-lulu@redhat.com> In-Reply-To: <20231103171641.1703146-1-lulu@redhat.com> References: <20231103171641.1703146-1-lulu@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add the check for iommufd_ictx,If vdpa don't have the iommufd_ictx then will use the Legacy iommu domain pathway Signed-off-by: Cindy Lu --- drivers/vhost/vdpa.c | 43 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c index dfaddd833364..0e2dba59e1ce 100644 --- a/drivers/vhost/vdpa.c +++ b/drivers/vhost/vdpa.c @@ -1067,9 +1067,6 @@ static int vhost_vdpa_map(struct vhost_vdpa *v, struc= t vhost_iotlb *iotlb, /* Legacy iommu domain pathway without IOMMUFD */ r =3D iommu_map(v->domain, iova, pa, size, perm_to_iommu_flags(perm), GFP_KERNEL); - } else { - r =3D iommu_map(v->domain, iova, pa, size, - perm_to_iommu_flags(perm), GFP_KERNEL); } if (r) { vhost_iotlb_del_range(iotlb, iova, iova + size - 1); @@ -1095,8 +1092,10 @@ static void vhost_vdpa_unmap(struct vhost_vdpa *v, if (ops->set_map) { if (!v->in_batch) ops->set_map(vdpa, asid, iotlb); + } else if (!vdpa->iommufd_ictx) { + /* Legacy iommu domain pathway without IOMMUFD */ + iommu_unmap(v->domain, iova, size); } - } =20 static int vhost_vdpa_va_map(struct vhost_vdpa *v, @@ -1149,7 +1148,36 @@ static int vhost_vdpa_va_map(struct vhost_vdpa *v, =20 return ret; } +#if 0 +int vhost_pin_pages(struct vdpa_device *device, dma_addr_t iova, int npage, + int prot, struct page **pages) +{ + if (!pages || !npage) + return -EINVAL; + //if (!device->config->dma_unmap) + //return -EINVAL; + + if (0) { //device->iommufd_access) { + int ret; + + if (iova > ULONG_MAX) + return -EINVAL; =20 + ret =3D iommufd_access_pin_pages( + device->iommufd_access, iova, npage * PAGE_SIZE, pages, + (prot & IOMMU_WRITE) ? IOMMUFD_ACCESS_RW_WRITE : 0); + if (ret) { + + return ret; + } + + return npage; + } else { + return pin_user_pages(iova, npage, prot, pages); + } + return -EINVAL; +} +#endif static int vhost_vdpa_pa_map(struct vhost_vdpa *v, struct vhost_iotlb *iotlb, u64 iova, u64 size, u64 uaddr, u32 perm) @@ -1418,9 +1446,13 @@ static void vhost_vdpa_free_domain(struct vhost_vdpa= *v) struct device *dma_dev =3D vdpa_get_dma_dev(vdpa); =20 if (v->domain) { - iommu_detach_device(v->domain, dma_dev); + if (!vdpa->iommufd_ictx) { + iommu_detach_device(v->domain, dma_dev); + } iommu_domain_free(v->domain); } + if (vdpa->iommufd_ictx) + vdpa_iommufd_unbind(vdpa); =20 v->domain =3D NULL; } @@ -1645,6 +1677,7 @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa) } =20 atomic_set(&v->opened, 0); + atomic_set(&vdpa->iommufd_users, 0); v->minor =3D minor; v->vdpa =3D vdpa; v->nvqs =3D vdpa->nvqs; --=20 2.34.3 From nobody Wed Dec 31 11:09:43 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EF63CC4167D for ; Fri, 3 Nov 2023 17:18:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376478AbjKCRSk (ORCPT ); Fri, 3 Nov 2023 13:18:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54240 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234408AbjKCRSg (ORCPT ); Fri, 3 Nov 2023 13:18:36 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 147BA1BD for ; Fri, 3 Nov 2023 10:17:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699031862; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ZVS2kk0D6Tt1PgwcrwV42T54QxmIjT+wF7r94StjHU4=; b=HYpS2zJgWieuA97Fnr0ELmzm+Tb4O+/uvGMRB46qYiZ958QhhXCsaaFxGKuTu3U93yDyDb S5HO4YsvvAtnYh0NOzYv89DLrf4uFyV29eALnFpvAWxvZePNyoKtUMesYhnqOyMHbbfktC V0XcLO4nZcfeIyZLzIqqGk5ougg1NXk= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-533-lM2HJKEnOZCxb--cVZ2jKw-1; Fri, 03 Nov 2023 13:17:40 -0400 X-MC-Unique: lM2HJKEnOZCxb--cVZ2jKw-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3EFB8282478F; Fri, 3 Nov 2023 17:17:36 +0000 (UTC) Received: from server.redhat.com (unknown [10.72.112.41]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1A404C1290F; Fri, 3 Nov 2023 17:17:32 +0000 (UTC) From: Cindy Lu To: lulu@redhat.com, jasowang@redhat.com, mst@redhat.com, yi.l.liu@intel.com, jgg@nvidia.com, linux-kernel@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org Subject: [RFC v1 7/8] vp_vdpa::Add support for iommufd Date: Sat, 4 Nov 2023 01:16:40 +0800 Message-Id: <20231103171641.1703146-8-lulu@redhat.com> In-Reply-To: <20231103171641.1703146-1-lulu@redhat.com> References: <20231103171641.1703146-1-lulu@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add new vdpa_config_ops function to support iommufd Signed-off-by: Cindy Lu --- drivers/vdpa/virtio_pci/vp_vdpa.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/vdpa/virtio_pci/vp_vdpa.c b/drivers/vdpa/virtio_pci/vp= _vdpa.c index 281287fae89f..dd2c372d36a6 100644 --- a/drivers/vdpa/virtio_pci/vp_vdpa.c +++ b/drivers/vdpa/virtio_pci/vp_vdpa.c @@ -460,6 +460,10 @@ static const struct vdpa_config_ops vp_vdpa_ops =3D { .set_config =3D vp_vdpa_set_config, .set_config_cb =3D vp_vdpa_set_config_cb, .get_vq_irq =3D vp_vdpa_get_vq_irq, + .bind_iommufd =3D vdpa_iommufd_physical_bind, + .unbind_iommufd =3D vdpa_iommufd_physical_unbind, + .attach_ioas =3D vdpa_iommufd_physical_attach_ioas, + .detach_ioas =3D vdpa_iommufd_physical_detach_ioas, }; =20 static void vp_vdpa_free_irq_vectors(void *data) --=20 2.34.3 From nobody Wed Dec 31 11:09:43 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 122BDC4332F for ; Fri, 3 Nov 2023 17:19:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377249AbjKCRTB (ORCPT ); Fri, 3 Nov 2023 13:19:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45978 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230202AbjKCRS7 (ORCPT ); Fri, 3 Nov 2023 13:18:59 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 17BF810C4 for ; Fri, 3 Nov 2023 10:18:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699031885; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=aCTB8Gg9xttEfEVtECz9fqwzez2GQHqNSW6hXp3Vsbk=; b=CncPKdsswMVl/lCkDzCSPz8NMM0ddK5mJGhnm6h4Ifs22e2h5RJCxw2EIu3CVAYrcaWIbC YZmIhr+aZYjuOZPeQjvz5Nyt74dqlugLRp+l2FHdAem9zxhynEnHqY0GbnmOhSOIXu9/XT qEhsJkkRjrD1U134h9n0HlGbj1z0w+o= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-161-HmhpnDUUNMKaWArsODPEXQ-1; Fri, 03 Nov 2023 13:18:02 -0400 X-MC-Unique: HmhpnDUUNMKaWArsODPEXQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id 902D33C16DDC; Fri, 3 Nov 2023 17:18:01 +0000 (UTC) Received: from server.redhat.com (unknown [10.72.112.41]) by smtp.corp.redhat.com (Postfix) with ESMTP id 63D381121309; Fri, 3 Nov 2023 17:17:58 +0000 (UTC) From: Cindy Lu To: lulu@redhat.com, jasowang@redhat.com, mst@redhat.com, yi.l.liu@intel.com, jgg@nvidia.com, linux-kernel@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org Subject: [RFC v1 8/8] iommu: expose the function iommu_device_use_default_domain Date: Sat, 4 Nov 2023 01:16:41 +0800 Message-Id: <20231103171641.1703146-9-lulu@redhat.com> In-Reply-To: <20231103171641.1703146-1-lulu@redhat.com> References: <20231103171641.1703146-1-lulu@redhat.com> 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.11.54.3 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Expose the function iommu_device_use_default_domain() and iommu_device_unuse_default_domain()=EF=BC=8C While vdpa bind the iommufd device and detach the iommu device, vdpa need to call the function iommu_device_unuse_default_domain() to release the owner Signed-off-by: Cindy Lu --- drivers/iommu/iommu.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 3bfc56df4f78..987cbf8c9a87 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -3164,6 +3164,7 @@ int iommu_device_use_default_domain(struct device *de= v) =20 return ret; } +EXPORT_SYMBOL_GPL(iommu_device_use_default_domain); =20 /** * iommu_device_unuse_default_domain() - Device driver stops handling devi= ce @@ -3187,6 +3188,7 @@ void iommu_device_unuse_default_domain(struct device = *dev) mutex_unlock(&group->mutex); iommu_group_put(group); } +EXPORT_SYMBOL_GPL(iommu_device_unuse_default_domain); =20 static int __iommu_group_alloc_blocking_domain(struct iommu_group *group) { --=20 2.34.3