Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp1684920rdh; Mon, 25 Sep 2023 23:35:39 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFExrg5QfMgLn2r+m+S7PO0nExqm2xiX/FpkCNXOI+og3qh6NfDbCXsDbrJKOYcqasBPm6W X-Received: by 2002:a05:6358:60c4:b0:139:cb15:ecd3 with SMTP id i4-20020a05635860c400b00139cb15ecd3mr9883709rwi.8.1695710138879; Mon, 25 Sep 2023 23:35:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695710138; cv=none; d=google.com; s=arc-20160816; b=n5jUGc6Mgo/+mCIfP80qhxn/x5jri/TYPjHpi5T5Ko6X7izVnYyCPyGJbgcXEkoOQS XZFNimDu4Z93a9Vb8Zsm7QuiTNq+H6fXsAz02uOy8/amcBu+vwP3gRnGVoGsDTaRv/9H hWkkxl3de9vvMOzrwZiOtMCvBIGDxTrXwpStB4u3WqYaPqBTbmysGPyBUcnsnxHWkNcM MsCy8Q6Mid/9UcNEVnqRC+YwOYKspNFjraFvrHl9Df2Jm674qtRp9SEA04ZYSnSZsvi1 XMd0fF34xlju59PAwr3ApSYAQ6j/lyzQuROG5YEhRizw9blPL4jXe7YwZfvcpIdJJ4pP VGMA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=e3cx0kxtLpPGv+5EBA2RQ4RZiZDVBT8lLjm6nn2c/2U=; fh=6wdPTxNhkXANRjy2JfZLp4xDVc6/eJgjSa8H+y4KSNY=; b=y2j4U4h5e/4neKmYIDCsBqP4akV2K2sVejp5XNjpzrAWUWpCk9oEqYGEvCNr8+fit8 +Pyr6Jte+Ts45Ym7JDykkEXGVbGj5KwBgLuyrjeZFrkKAC51u5pmSGHN6JQE6jm8fTRO cEK2k4oEyQjhYXn4iMWjmvUprfQ1Ibdof8ecihU3JwbWGhyvPHxscw12vB4U0/IyYq60 3YU0r4hDZDjay58HsWgU9ZmAbJINrErWdHwGzQztAK875KZjuTFxdRpzPvKBmeoEwAqy EODjoQZxI3jqVyKBIFh6KVA0mEGPRGFPdlhIRQPoyu5e2cJgS2A054leyAVDdAMBuAp6 FDQg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=eWFKEzOX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id fb35-20020a056a002da300b00690c22195c4si11977665pfb.0.2023.09.25.23.35.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 23:35:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=eWFKEzOX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 235A58251545; Mon, 25 Sep 2023 19:48:34 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233549AbjIZCse (ORCPT + 99 others); Mon, 25 Sep 2023 22:48:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49886 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233554AbjIZCsb (ORCPT ); Mon, 25 Sep 2023 22:48:31 -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 642D010A for ; Mon, 25 Sep 2023 19:47:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1695696459; 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=e3cx0kxtLpPGv+5EBA2RQ4RZiZDVBT8lLjm6nn2c/2U=; b=eWFKEzOXQ7A+uVMTTl20FaYo27NuY0gwHE2H3TCmXUCtmEExGUJQ2dusDVqBEU3/085gcd godoSu5gMPTc9S+N8k/Uf6Y1dXrqm/bINWtrJThtxM38UNIpyU1OhJuZpeSPNLYo2lWyrX kYUhq2R4VkCvqsYdyBFkFO0S87kgpnI= Received: from mail-lf1-f71.google.com (mail-lf1-f71.google.com [209.85.167.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-694-J92cZqtQPkaXMctTPxYpZw-1; Mon, 25 Sep 2023 22:47:38 -0400 X-MC-Unique: J92cZqtQPkaXMctTPxYpZw-1 Received: by mail-lf1-f71.google.com with SMTP id 2adb3069b0e04-500a9156daaso10930793e87.0 for ; Mon, 25 Sep 2023 19:47:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695696456; x=1696301256; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=e3cx0kxtLpPGv+5EBA2RQ4RZiZDVBT8lLjm6nn2c/2U=; b=jff7hwLuDZEf0nK5FEffZVJF3nAM3yI8lK9RA/yaDoHZd+RNcCm8tPkN4y09k052xZ JNIiHFfrxGbgF8wXjJvJOYhuOQzXrb2MN3SsU1xZMKSpbSMfa32aAOPi7jSJE1f+mpW1 v/UhbKFlgkpgEqf+RniP4pSK8ULdpKzdBG06JDPjqVnKM6ZnTxlsbifZGnROx73S21Zc uqCbPl6NQE5z+D938v8LRIpF6DWkXooa/vTG8yoG4mQIGRlMYkRyifvwXhRFdmtW0Ups eNV1iGn6+O3mAoBochtasqX2GRcZjBSmpdURNSolwZuL56oEcQE0hB64/IH0aiw276QW r+rA== X-Gm-Message-State: AOJu0YwwRoTH/94t6Y56JXw+UvJvE5g8VbcPYuecVaUcF65ek89mAoge VRz8q76vYAOQvn1NriMZivo6nRdprU3HSMkkvGHa1Jf78rvzq99JKMkGR05Xnd9pUxpqQyb7Yqt 7SfTRtD24X73PbjysPdQqH1BmAKpYl6kB6zXOnliP X-Received: by 2002:ac2:484a:0:b0:504:3807:22a4 with SMTP id 10-20020ac2484a000000b00504380722a4mr6579867lfy.23.1695696456741; Mon, 25 Sep 2023 19:47:36 -0700 (PDT) X-Received: by 2002:ac2:484a:0:b0:504:3807:22a4 with SMTP id 10-20020ac2484a000000b00504380722a4mr6579862lfy.23.1695696456554; Mon, 25 Sep 2023 19:47:36 -0700 (PDT) MIME-Version: 1.0 References: <20230923170540.1447301-1-lulu@redhat.com> <20230923170540.1447301-2-lulu@redhat.com> In-Reply-To: <20230923170540.1447301-2-lulu@redhat.com> From: Jason Wang Date: Tue, 26 Sep 2023 10:47:25 +0800 Message-ID: Subject: Re: [RFC 1/7] vhost/iommufd: Add the functions support iommufd To: Cindy Lu Cc: 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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Mon, 25 Sep 2023 19:48:34 -0700 (PDT) On Sun, Sep 24, 2023 at 1:05=E2=80=AFAM Cindy Lu wrote: > > 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 | 151 ++++++++++++++++++++++++++++++++++++++++ > drivers/vhost/vhost.h | 21 ++++++ > 2 files changed, 172 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..080858f76fd5 > --- /dev/null > +++ b/drivers/vhost/iommufd.c > @@ -0,0 +1,151 @@ > +#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 =3D vdpa->config->bind_iommufd(vdpa, ictx, device_id); > + if (ret) > + return ret; > + > + ret =3D vdpa->config->attach_ioas(vdpa, ioas_id); > + if (ret) > + goto err_unbind; > + vdpa->iommufd_attached =3D true; > + > + return 0; > + > +err_unbind: > + if (vdpa->config->unbind_iommufd) > + vdpa->config->unbind_iommufd(vdpa); > + return ret; > +} > + > +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 *pt_= id) > +{ > + unsigned int flags =3D 0; > + > + return iommufd_device_attach(vdpa->iommufd_device, pt_id); > +} > +EXPORT_SYMBOL_GPL(vdpa_iommufd_physical_attach_ioas); > + > +static void vdpa_emulated_unmap(void *data, unsigned long iova, > + unsigned long length) > +{ > + struct vdpa_device *vdpa =3D data; > + > + vdpa->config->dma_unmap(vdpa, 0, iova, length); > +} > + > +static const struct iommufd_access_ops vdpa_user_ops =3D { > + .needs_pin_pages =3D 1, Note that simulators support VA, so no page pinning in that case while reba= sing. static bool use_va =3D true; module_param(use_va, bool, 0444); MODULE_PARM_DESC(use_va, "Enable/disable the device's ability to use VA"); So we need to handle that case as well. (Note that it looks like VA mode is broken, I may need some time to fix tha= t). > + .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); > + > + vdpa->iommufd_ictx =3D ictx; > + iommufd_ctx_get(ictx); > + struct iommufd_device *idev; > + > + idev =3D iommufd_device_bind(ictx, vdpa->dma_dev, out_device_id); This seems not appropriate for emulated devices as it deals with the concepts that only exist in physical devices like the IOMMU domain etc. If possible, please refer how VFIO handles this (I guess it should have something) Thanks