From patchwork Mon Oct 9 08:51:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 731263 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 82EFAE95A99 for ; Mon, 9 Oct 2023 08:52:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345570AbjJIIvg (ORCPT ); Mon, 9 Oct 2023 04:51:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46822 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345577AbjJIIvb (ORCPT ); Mon, 9 Oct 2023 04:51:31 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 36E9ED8; Mon, 9 Oct 2023 01:51:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1696841488; x=1728377488; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=oW307Sz3JLhBJDdKvXdymtYFyyVye4wCg0yXRx4S4UE=; b=DrquFYH3kHqbyy/zHQxiY7t75YyhKn/89vJnDpro4QThddoKWYUHmd7a xyx3Sa2sjjJADFgXE+6gAcsTloCcRK9eJAp/mH7Qgx+ZIr4x+eWRc6MM9 YY3vC5+F/407ap6m57FvSZy5eB4rHORytK8r4dRpcsbwHPJqivK/KCBwM 3OgMIfEL5eHQS8jXwu1MXVYGvPh0Mm/xI0gHw+R+loId781qTcsmmUqTW W0oGXa+uAnh9GhZY9G1NMQR67NkbGauTosLM9E3Amk93+kOBhQF/f4+y8 h26OZex0JN7NssA2GiNNNoXsTNoHborUvqblhiTqP7TZVUNjBnHyhK5FM A==; X-IronPort-AV: E=McAfee;i="6600,9927,10857"; a="364390755" X-IronPort-AV: E=Sophos;i="6.03,209,1694761200"; d="scan'208";a="364390755" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Oct 2023 01:51:27 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10857"; a="818781645" X-IronPort-AV: E=Sophos;i="6.03,209,1694761200"; d="scan'208";a="818781645" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga008.fm.intel.com with ESMTP; 09 Oct 2023 01:51:27 -0700 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com, joao.m.martins@oracle.com Subject: [RFC 1/7] iommufd: Handle unsafe interrupts in a separate function Date: Mon, 9 Oct 2023 01:51:17 -0700 Message-Id: <20231009085123.463179-2-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231009085123.463179-1-yi.l.liu@intel.com> References: <20231009085123.463179-1-yi.l.liu@intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Kevin Tian This wraps the unsafe interrupts handling into helper as same check is also required when supporting iommufd_device_bind_pasid() later. Signed-off-by: Kevin Tian Signed-off-by: Yi Liu --- drivers/iommu/iommufd/device.c | 36 ++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 6a6145b4a25e..ca3919fecc89 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -145,6 +145,25 @@ void iommufd_device_destroy(struct iommufd_object *obj) iommufd_ctx_put(idev->ictx); } +/* + * For historical compat with VFIO the insecure interrupt path is + * allowed if the module parameter is set. Insecure means that a MemWr + * operation from the device (eg a simple DMA) cannot trigger an + * interrupt outside this iommufd context. + */ +static int iommufd_allow_unsafe_interrupts(struct device *dev) +{ + if (!allow_unsafe_interrupts) + return -EPERM; + + dev_warn( + dev, + "MSI interrupts are not secure, they cannot be isolated by the platform. " + "Check that platform features like interrupt remapping are enabled. " + "Use the \"allow_unsafe_interrupts\" module parameter to override\n"); + return 0; +} + /** * iommufd_device_bind - Bind a physical device to an iommu fd * @ictx: iommufd file descriptor @@ -179,24 +198,11 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, if (IS_ERR(igroup)) return ERR_CAST(igroup); - /* - * For historical compat with VFIO the insecure interrupt path is - * allowed if the module parameter is set. Secure/Isolated means that a - * MemWr operation from the device (eg a simple DMA) cannot trigger an - * interrupt outside this iommufd context. - */ if (!iommufd_selftest_is_mock_dev(dev) && !iommu_group_has_isolated_msi(igroup->group)) { - if (!allow_unsafe_interrupts) { - rc = -EPERM; + rc = iommufd_allow_unsafe_interrupts(dev); + if (rc) goto out_group_put; - } - - dev_warn( - dev, - "MSI interrupts are not secure, they cannot be isolated by the platform. " - "Check that platform features like interrupt remapping are enabled. " - "Use the \"allow_unsafe_interrupts\" module parameter to override\n"); } rc = iommu_device_claim_dma_owner(dev, ictx); From patchwork Mon Oct 9 08:51:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 731265 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 15103E95A97 for ; Mon, 9 Oct 2023 08:51:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345623AbjJIIvn (ORCPT ); Mon, 9 Oct 2023 04:51:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46882 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345609AbjJIIvg (ORCPT ); Mon, 9 Oct 2023 04:51:36 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0022F102; Mon, 9 Oct 2023 01:51:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1696841493; x=1728377493; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=1yMPZv0+hy/FoeMbAtBKKDM1b/wty0WUySVOG2Kdxzs=; b=OU+rXxQOXeqqApHuqeAtYdMWE8prkmHe58jlq9BFXir9LqfW8WBN1o/A s9n1LIlJ6L+lFr42tRI6QQpraLt9m1+HJE6BD3It8fqQyV3cIS3/r13F+ a/mt+rRJ/TRWQaF4G+hSF1vzNT/rPmWqMPlfOImlTnibjgnnR1PHfJwZ0 wtLxQN23oKVCuq3bvmm1yKgkKaaqp08LyjkzCNNKUgIUw7D5pGSp6UkIa j29RukifBR8dW4AZVMTZTBlL70ZXkEnt3HzAkd14LolcNnMASqXqVZ30Z gHpFbkOCMxVowr5eL+GueYnkTzjVUbADgctkHnPCBlE00pP9Lw+IHRpC+ Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10857"; a="364390800" X-IronPort-AV: E=Sophos;i="6.03,209,1694761200"; d="scan'208";a="364390800" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Oct 2023 01:51:32 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10857"; a="818781671" X-IronPort-AV: E=Sophos;i="6.03,209,1694761200"; d="scan'208";a="818781671" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga008.fm.intel.com with ESMTP; 09 Oct 2023 01:51:31 -0700 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com, joao.m.martins@oracle.com Subject: [RFC 3/7] iommufd: Add iommufd_device_bind_pasid() Date: Mon, 9 Oct 2023 01:51:19 -0700 Message-Id: <20231009085123.463179-4-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231009085123.463179-1-yi.l.liu@intel.com> References: <20231009085123.463179-1-yi.l.liu@intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Kevin Tian Intel SIOV allows creating virtual devices of which the vRID is represented by a pasid of a physical device. So such device can be bound to an iommufd as physical device does and then later be attached to an IOAS/hwpt using that pasid. Binding a virtual device has different security contract comparing to binding a physical device. There is no DMA ownership claim per pasid since the parent device is already claimed by the parent driver including the entire pasid space. With that we simply store the pasid in the object once it passes other checks. Signed-off-by: Kevin Tian Signed-off-by: Yi Liu --- drivers/iommu/iommufd/device.c | 72 ++++++++++++++++++++++++- drivers/iommu/iommufd/iommufd_private.h | 7 +++ include/linux/iommufd.h | 3 ++ 3 files changed, 80 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 9dd76d92b7f6..35c1419ee96b 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -5,6 +5,7 @@ #include #include #include +#include #include "../iommu-priv.h" #include "io_pagetable.h" @@ -139,8 +140,10 @@ void iommufd_device_destroy(struct iommufd_object *obj) WARN_ON(!xa_empty(&idev->pasid_hwpts)); if (idev->has_user_data) dev_iommu_ops(idev->dev)->unset_dev_user_data(idev->dev); - iommu_device_release_dma_owner(idev->dev); - iommufd_put_group(idev->igroup); + if (idev->igroup) { + iommu_device_release_dma_owner(idev->dev); + iommufd_put_group(idev->igroup); + } if (!iommufd_selftest_is_mock_dev(idev->dev)) iommufd_ctx_put(idev->ictx); } @@ -257,6 +260,71 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, } EXPORT_SYMBOL_NS_GPL(iommufd_device_bind, IOMMUFD); +/** + * iommufd_device_bind_pasid - Bind a virtual device to an iommu fd + * @ictx: iommufd file descriptor + * @dev: Pointer to the parent physical device struct + * @pasid: the pasid value representing vRID of this virtual device + * @id: Output ID number to return to userspace for this device + * + * The virtual device always tags its DMA with the provided pasid. + * A successful bind allows the pasid to be used in other iommufd + * operations e.g. attach/detach and returns struct iommufd_device + * pointer, otherwise returns error pointer. + * + * There is no ownership check per pasid. A driver using this API + * must already claim the DMA ownership over the parent device and + * the pasid is allocated by the driver itself. + * + * PASID is a device capability so unlike iommufd_device_bind() it + * has no iommu group associated. + * + * The caller must undo this with iommufd_device_unbind() + */ +struct iommufd_device *iommufd_device_bind_pasid(struct iommufd_ctx *ictx, + struct device *dev, + u32 pasid, u32 *id) +{ + struct iommufd_device *idev; + int rc; + + /* + * iommufd always sets IOMMU_CACHE because we offer no way for userspace + * to restore cache coherency. + */ + if (!device_iommu_capable(dev, IOMMU_CAP_CACHE_COHERENCY)) + return ERR_PTR(-EINVAL); + + /* + * No iommu supports pasid-granular msi message today. Here we + * just check whether the parent device can do safe interrupts. + * Isolation between virtual devices within the parent device + * relies on the parent driver to enforce. + */ + if (!iommufd_selftest_is_mock_dev(dev) && + !msi_device_has_isolated_msi(dev)) { + rc = iommufd_allow_unsafe_interrupts(dev); + if (rc) + return ERR_PTR(rc); + } + + idev = iommufd_alloc_device(ictx, dev); + if (IS_ERR(idev)) + return idev; + idev->default_pasid = pasid; + + /* + * If the caller fails after this success it must call + * iommufd_unbind_device() which is safe since we hold this refcount. + * This also means the device is a leaf in the graph and no other + * object can take a reference on it. + */ + iommufd_object_finalize(ictx, &idev->obj); + *id = idev->obj.id; + return idev; +} +EXPORT_SYMBOL_NS_GPL(iommufd_device_bind_pasid, IOMMUFD); + /** * iommufd_ctx_has_group - True if any device within the group is bound * to the ictx diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 06ebee4c87c5..7b3405fd6fd3 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -332,10 +332,17 @@ struct iommufd_group { struct iommufd_device { struct iommufd_object obj; struct iommufd_ctx *ictx; + /* valid if this is a physical device */ struct iommufd_group *igroup; struct list_head group_item; /* always the physical device */ struct device *dev; + /* + * valid if this is a virtual device which gains pasid-granular + * DMA isolation in IOMMU. The default pasid is used when attaching + * this device to a IOAS/hwpt. + */ + u32 default_pasid; struct xarray pasid_hwpts; bool enforce_cache_coherency; bool has_user_data; diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h index 0b007c376306..402320d6eba1 100644 --- a/include/linux/iommufd.h +++ b/include/linux/iommufd.h @@ -20,6 +20,9 @@ struct iommu_group; struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, struct device *dev, u32 *id); +struct iommufd_device *iommufd_device_bind_pasid(struct iommufd_ctx *ictx, + struct device *dev, + u32 pasid, u32 *id); void iommufd_device_unbind(struct iommufd_device *idev); int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id); From patchwork Mon Oct 9 08:51:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 731264 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 3ADA4E95A96 for ; Mon, 9 Oct 2023 08:51:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345692AbjJIIvy (ORCPT ); Mon, 9 Oct 2023 04:51:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45872 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345641AbjJIIvl (ORCPT ); Mon, 9 Oct 2023 04:51:41 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5E9BBED; Mon, 9 Oct 2023 01:51:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1696841498; x=1728377498; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=r/ghgXXFlCC5X+m363LcA6Jn28Rpf678n/rxTiOLSIg=; b=ZO90lCD+bDqiDh0qMA4Dq9b6XnY+fYZXUDyLyD5zf5sApFTqYBjQmV04 lJ8qMHzBWDO75wrRddpdIKOc1jTAT8Sou5qquJM2KreQZlv2UO1d7LHoa wCylcVtRGnnTzKZWIn3dTCdPSSnDiDOYJau5Or1kBtX0M9z9FcSwfmvuQ n0h12aGyCy5Cl2epS+e3PFwHFfwIVtkIcvuh2RZ2brDh7AQUwcq+QT7n5 FzlNYWX/ajEm6+cPvUol83ODoS/05y8ye2okgWuVGVPZtYxwU57pKaj3d pxMU4SxyTAesSSs3EKNexAiMvwb7rdZ4/PWtpmLxb4w6f9K7hOzfPrU1y g==; X-IronPort-AV: E=McAfee;i="6600,9927,10857"; a="364390846" X-IronPort-AV: E=Sophos;i="6.03,209,1694761200"; d="scan'208";a="364390846" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Oct 2023 01:51:37 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10857"; a="818781714" X-IronPort-AV: E=Sophos;i="6.03,209,1694761200"; d="scan'208";a="818781714" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga008.fm.intel.com with ESMTP; 09 Oct 2023 01:51:36 -0700 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com, joao.m.martins@oracle.com Subject: [RFC 6/7] iommufd/selftest: Add test coverage for SIOV virtual device Date: Mon, 9 Oct 2023 01:51:22 -0700 Message-Id: <20231009085123.463179-7-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231009085123.463179-1-yi.l.liu@intel.com> References: <20231009085123.463179-1-yi.l.liu@intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org This adds test coverage for SIOV virtual device by passsing a non-zero pasid to IOMMU_TEST_OP_MOCK_DOMAIN op, and check if the SIOV virtual device (a.k.a pasid of this device) is attached to the mock domain, then tries to replace with a new hwpt and other types of hwpts, and check if the attached domain of this virtual device is correct. Signed-off-by: Yi Liu --- tools/testing/selftests/iommu/iommufd.c | 53 ++++++++++++++++++- .../selftests/iommu/iommufd_fail_nth.c | 26 +++++++++ 2 files changed, 77 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c index 9a1fbba89e96..945ab07a8b84 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -2031,14 +2031,20 @@ FIXTURE(iommufd_device_pasid) uint32_t device_id; }; +FIXTURE_VARIANT(iommufd_device_pasid) +{ + uint32_t pasid; +}; + FIXTURE_SETUP(iommufd_device_pasid) { self->fd = open("/dev/iommu", O_RDWR); ASSERT_NE(-1, self->fd); test_ioctl_ioas_alloc(&self->ioas_id); - test_cmd_mock_domain(self->ioas_id, 0, &self->stdev_id, - &self->hwpt_id, &self->device_id); + test_cmd_mock_domain(self->ioas_id, variant->pasid, + &self->stdev_id, &self->hwpt_id, + &self->device_id); } FIXTURE_TEARDOWN(iommufd_device_pasid) @@ -2046,6 +2052,12 @@ FIXTURE_TEARDOWN(iommufd_device_pasid) teardown_iommufd(self->fd, _metadata); } +/* For SIOV test */ +FIXTURE_VARIANT_ADD(iommufd_device_pasid, siov_pasid_600) +{ + .pasid = 600, //this is the default pasid for the SIOV virtual device +}; + TEST_F(iommufd_device_pasid, pasid_attach) { if (self->device_id) { @@ -2071,6 +2083,43 @@ TEST_F(iommufd_device_pasid, pasid_attach) IOMMU_HWPT_ALLOC_DATA_SELFTEST, &data, sizeof(data)); + if (variant->pasid) { + uint32_t new_hwpt_id = 0; + + ASSERT_EQ(0, + test_cmd_pasid_check_domain(self->fd, + self->stdev_id, + variant->pasid, + self->hwpt_id, + &result)); + EXPECT_EQ(1, result); + test_cmd_hwpt_alloc(self->device_id, self->ioas_id, + 0, &new_hwpt_id); + test_cmd_mock_domain_replace(self->stdev_id, + new_hwpt_id); + ASSERT_EQ(0, + test_cmd_pasid_check_domain(self->fd, + self->stdev_id, + variant->pasid, + new_hwpt_id, + &result)); + EXPECT_EQ(1, result); + + /* + * Detach hwpt from variant->pasid, and check if the + * variant->pasid has null domain + */ + test_cmd_pasid_detach(variant->pasid); + ASSERT_EQ(0, + test_cmd_pasid_check_domain(self->fd, + self->stdev_id, + variant->pasid, + 0, &result)); + EXPECT_EQ(1, result); + + test_ioctl_destroy(new_hwpt_id); + } + /* * Attach ioas to pasid 100, should succeed, domain should * be valid. diff --git a/tools/testing/selftests/iommu/iommufd_fail_nth.c b/tools/testing/selftests/iommu/iommufd_fail_nth.c index 691903c63de0..a5fb45d99869 100644 --- a/tools/testing/selftests/iommu/iommufd_fail_nth.c +++ b/tools/testing/selftests/iommu/iommufd_fail_nth.c @@ -644,6 +644,32 @@ TEST_FAIL_NTH(basic_fail_nth, device) self->pasid = 0; + if (_test_ioctl_destroy(self->fd, self->stdev_id)) + return -1; + + self->pasid = 300; + self->stdev_id = 0; + + /* Test for SIOV virtual devices attach */ + if (_test_cmd_mock_domain(self->fd, ioas_id, self->pasid, + &self->stdev_id, NULL, &idev_id)) + return -1; + + /* Test for SIOV virtual device replace */ + if (_test_cmd_mock_domain_replace(self->fd, self->stdev_id, + hwpt_id, NULL)) + return -1; + + if (_test_cmd_pasid_detach(self->fd, self->stdev_id, self->pasid)) + return -1; + + self->pasid = 0; + + if (_test_ioctl_destroy(self->fd, self->stdev_id)) + return -1; + + self->stdev_id = 0; + return 0; }