From patchwork Fri Nov 3 17:16:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Cindy Lu X-Patchwork-Id: 13444792 Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (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 082FA20309 for ; Fri, 3 Nov 2023 17:16:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="DXAglskq" 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 A89FCD4C 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> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 X-Patchwork-State: RFC Add a new file vhost/iommufd.c to support the function of iommufd, This file contains iommufd function of emulated device and the physical device.  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 does + * not do DMA at all. So nothing to do. + */ + if (!vdpa->config->bind_iommufd) + return 0; + ret = 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 = vdpa_get_dma_dev(vdpa); + struct iommufd_device *idev; + + idev = iommufd_device_bind(ictx, dma_dev, out_device_id); + if (IS_ERR(idev)) + return PTR_ERR(idev); + vdpa->iommufd_device = 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 = false; + } + iommufd_device_unbind(vdpa->iommufd_device); + vdpa->iommufd_device = 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 = iommufd_device_replace(vdpa->iommufd_device, + iommufd_ioasid); + else + rc = iommufd_device_attach(vdpa->iommufd_device, + iommufd_ioasid); + if (rc) + return rc; + vdpa->iommufd_attached = 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 = 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 = 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 = { + .needs_pin_pages = 1, + .unmap = 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 = iommufd_access_create(ictx, &vdpa_user_ops, vdpa, out_device_id); + if (IS_ERR(user)) + return PTR_ERR(user); + vdpa->iommufd_access = 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 = false; + vdpa->iommufd_access = 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 = iommufd_access_replace(vdpa->iommufd_access, + *iommufd_ioasid); + } else { + rc = iommufd_access_attach(vdpa->iommufd_access, + *iommufd_ioasid); + } + user = vdpa->iommufd_access; + + if (rc) + return rc; + vdpa->iommufd_attached = 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 = 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 +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 val) { From patchwork Fri Nov 3 17:16:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cindy Lu X-Patchwork-Id: 13444793 Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (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 19FC62033A for ; Fri, 3 Nov 2023 17:17:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="fu28+IpF" 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 2843C13E for ; Fri, 3 Nov 2023 10:17:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699031819; 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=fu28+IpFlQas8mxoN7TZI/8oTlMrWcZYpnt6UylHAEmX+yk1vJPXbOuCWM5Cn4Hwdvn2lJ xEocG4g2l04WWFVR/kkZzIQ2JiSLAJzhnqypnfPOLYMbt67upvcufHLRKQEdRS7iy9gwor +CYH9DiCBJBqoKymMzQpNksCLu9FhUk= 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> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 X-Patchwork-State: RFC 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) += vringh.o obj-$(CONFIG_VHOST_VDPA) += vhost_vdpa.o vhost_vdpa-y := vdpa.o +vhost_vdpa-$(CONFIG_IOMMUFD) += iommufd.o obj-$(CONFIG_VHOST) += vhost.o From patchwork Fri Nov 3 17:16:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cindy Lu X-Patchwork-Id: 13444794 Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (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 827871D6AA for ; Fri, 3 Nov 2023 17:17:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="ENK+mJ1n" 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 E3C90D4C 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> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 X-Patchwork-State: RFC 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 @@ #include "vhost.h" +MODULE_IMPORT_NS(IOMMUFD); + enum { VHOST_VDPA_BACKEND_FEATURES = (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); +void vhost_vdpa_lockdep_assert_held(struct vdpa_device *vdpa) +{ + struct vhost_vdpa *v = 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 = container_of(iotlb, struct @@ -551,6 +563,149 @@ static long vhost_vdpa_suspend(struct vhost_vdpa *v) return ops->suspend(vdpa); } +static long vhost_vdpa_iommufd_set_device(struct vhost_vdpa *v, + void __user *argp) +{ + struct device *dma_dev = vdpa_get_dma_dev(v->vdpa); + struct vhost_vdpa_set_iommufd set_iommufd; + struct vdpa_device *vdpa = v->vdpa; + struct iommufd_ctx *ictx; + unsigned long minsz; + u32 ioas_id, dev_id; + struct fd f; + long r = 0; + + minsz = 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 = NULL; + vdpa->iommufd_ictx = 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 = -EBADF; + f = fdget(set_iommufd.iommufd); + if (!f.file) + goto out; + + r = -EINVAL; + ictx = 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 = set_iommufd.iommufd_ioasid; + r = vdpa_iommufd_bind(vdpa, ictx, &ioas_id, &dev_id); + if (r) + goto out_reattach; + + set_iommufd.out_dev_id = dev_id; + r = 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 = 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 = 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 = -EINVAL; + return ret; + } + + ret = v->vdpa->config->attach_ioas(v->vdpa, &attach.ioas_id); + + if (ret) + return ret; + + ret = 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 = 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); +} /* After a successful return of this ioctl the device resumes processing * virtqueue descriptors. The device becomes fully operational the same way it @@ -744,6 +899,18 @@ static long vhost_vdpa_unlocked_ioctl(struct file *filep, case VHOST_SET_LOG_FD: r = -ENOIOCTLCMD; break; + case VHOST_VDPA_SET_IOMMU_FD: + + r = vhost_vdpa_iommufd_set_device(v, argp); + break; + case VDPA_DEVICE_ATTACH_IOMMUFD_AS: + r = vhost_vdpa_iommufd_ioas_attach(v, (void __user *)arg); + break; + + case VDPA_DEVICE_DETACH_IOMMUFD_AS: + r = vhost_vdpa_iommufd_ioas_detach(v, (void __user *)arg); + break; + case VHOST_VDPA_SET_CONFIG_CALL: r = vhost_vdpa_set_config_call(v, argp); break; @@ -896,6 +1063,10 @@ static int vhost_vdpa_map(struct vhost_vdpa *v, struct vhost_iotlb *iotlb, } else if (ops->set_map) { if (!v->in_batch) r = ops->set_map(vdpa, asid, iotlb); + } else if (!vdpa->iommufd_ictx) { + /* Legacy iommu domain pathway without IOMMUFD */ + r = iommu_map(v->domain, iova, pa, size, + perm_to_iommu_flags(perm), GFP_KERNEL); } else { r = 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) +/* 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 From patchwork Fri Nov 3 17:16:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cindy Lu X-Patchwork-Id: 13444795 Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (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 EE71E1D6AA for ; Fri, 3 Nov 2023 17:17:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="cB08SaEW" 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 87DF913E for ; Fri, 3 Nov 2023 10:17:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699031849; 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=cB08SaEWhkrRma2eKY9izcfqogqRUfX50J8A7zBrRmkNLUZ45Ql6ABZVsljAXD1+mmtHnS OHLArpBYZWRhr8YSoyhYkfLDwtmqwx0U4NW807pn0oli9bX/MfEET2WOY2iI63NnkQlUrS qyLVecc+NaQPPdA+mEmMp8ZLgjCIGRE= 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> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 X-Patchwork-State: RFC 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; }; /** @@ -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 IOMMU + * 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 { /* 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); + }; 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); -#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 From patchwork Fri Nov 3 17:16:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cindy Lu X-Patchwork-Id: 13444796 Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (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 43E921D6AA for ; Fri, 3 Nov 2023 17:17:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Ft82Hqy/" 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 147DA13E 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=1699031858; 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=Ft82Hqy/4WcFtuAeWR38lbEEwAPOyA/quiH5giIYxxcDMo5tl7Bos2aVqET45nbIrknDUt vVPN9HdYAKv9Afz6KKIaqg1gKJLIXf0h+9r7h69ks2/r64nGBwP6KDsc6u6mNjCHM9SVc/ 0Xbjcrp6Nn6i+p0ekDvRlG1ewN47q2k= 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> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 X-Patchwork-State: RFC 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 = { .bind_mm = vdpasim_bind_mm, .unbind_mm = vdpasim_unbind_mm, .free = vdpasim_free, + .bind_iommufd = vdpa_iommufd_emulated_bind, + .unbind_iommufd = vdpa_iommufd_emulated_unbind, + .attach_ioas = vdpa_iommufd_emulated_attach_ioas, + .detach_ioas = vdpa_iommufd_emulated_detach_ioas, }; static const struct vdpa_config_ops vdpasim_batch_config_ops = { @@ -799,6 +803,10 @@ static const struct vdpa_config_ops vdpasim_batch_config_ops = { .bind_mm = vdpasim_bind_mm, .unbind_mm = vdpasim_unbind_mm, .free = vdpasim_free, + .bind_iommufd = vdpa_iommufd_emulated_bind, + .unbind_iommufd = vdpa_iommufd_emulated_unbind, + .attach_ioas = vdpa_iommufd_emulated_attach_ioas, + .detach_ioas = vdpa_iommufd_emulated_detach_ioas, }; MODULE_VERSION(DRV_VERSION); From patchwork Fri Nov 3 17:16:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cindy Lu X-Patchwork-Id: 13444798 Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (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 911B51F92C for ; Fri, 3 Nov 2023 17:17:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="du7Vsg27" 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 8B3C0D52 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=1699031861; 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=du7Vsg27mCeJWPwWBDTKsGu2O8ZcEXCnEaKP/jPy33mU8Tn9WyOMrxneRUNUzVRc0Xw6CD hYHkbUXXxmOzPZGw8EoQgpKP4njZGf6zniDTB5QToJGV/6cBng8RkpEJ3WQ1/M+hzuCveO bxa0iIuOrWlPPOQVpL33Y0KtRu1ZPs0= 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> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 X-Patchwork-State: RFC 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, struct vhost_iotlb *iotlb, /* Legacy iommu domain pathway without IOMMUFD */ r = iommu_map(v->domain, iova, pa, size, perm_to_iommu_flags(perm), GFP_KERNEL); - } else { - r = 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); } - } static int vhost_vdpa_va_map(struct vhost_vdpa *v, @@ -1149,7 +1148,36 @@ static int vhost_vdpa_va_map(struct vhost_vdpa *v, 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; + ret = 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 = vdpa_get_dma_dev(vdpa); 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); v->domain = NULL; } @@ -1645,6 +1677,7 @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa) } atomic_set(&v->opened, 0); + atomic_set(&vdpa->iommufd_users, 0); v->minor = minor; v->vdpa = vdpa; v->nvqs = vdpa->nvqs; From patchwork Fri Nov 3 17:16:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cindy Lu X-Patchwork-Id: 13444797 Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (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 186691D6AA for ; Fri, 3 Nov 2023 17:17:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="X7DG3s2W" 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 3DD44D50 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=1699031861; 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=X7DG3s2WrH2LjyfRjHHMdSxr6yYpCAL0dGNyCQYx+Dswa+tytPV4mTnYwoVukdAZYFZsLh 9zo/AQdioMhQZwRaRgCFDlUCkWIXOwzInHjjV3P74z2s/i1mKSvlTwql4YYiiUgybNop/H s9AwBBY0imxdaEo7n0MiCRWLLz2s6lg= 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> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 X-Patchwork-State: RFC 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 = { .set_config = vp_vdpa_set_config, .set_config_cb = vp_vdpa_set_config_cb, .get_vq_irq = vp_vdpa_get_vq_irq, + .bind_iommufd = vdpa_iommufd_physical_bind, + .unbind_iommufd = vdpa_iommufd_physical_unbind, + .attach_ioas = vdpa_iommufd_physical_attach_ioas, + .detach_ioas = vdpa_iommufd_physical_detach_ioas, }; static void vp_vdpa_free_irq_vectors(void *data) From patchwork Fri Nov 3 17:16:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Cindy Lu X-Patchwork-Id: 13444799 Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (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 81B901F92C for ; Fri, 3 Nov 2023 17:18:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="K+AJRNMW" 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 151F010CF for ; Fri, 3 Nov 2023 10:18:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699031887; 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=K+AJRNMWUq52CW31BiS0fG8SoJ0LMbani3aUuY2EeVU4lld6UT6sIEUj+2k8CRaz9T5gHQ spIrXHgPYxi9TNPCGqt0avgMWHEjojLnqSzZ6TFQ3rB+A9yqbQr2T4Fi/ghYrtKGrRJySd 06oljP2sEXWCfOiYfi4uxm6lKR12hr4= 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> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.3 X-Patchwork-State: RFC Expose the function iommu_device_use_default_domain() and iommu_device_unuse_default_domain(), 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 *dev) return ret; } +EXPORT_SYMBOL_GPL(iommu_device_use_default_domain); /** * iommu_device_unuse_default_domain() - Device driver stops handling device @@ -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); static int __iommu_group_alloc_blocking_domain(struct iommu_group *group) {