From nobody Thu Dec 18 18:00:52 2025 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.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 0654617966E for ; Tue, 30 Apr 2024 14:59:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.19 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714489160; cv=none; b=fXlIBJ/Uj4DKjvveiv9cqd4me5UjG2tPF5xCoRTs61NLTUKSDfoqu1ymoBwERdGnsL3wuWfyPYpKC/+ggBIKl8W0NxPoMBFGdx/IIk8fC+c7SIIa4KS6R5jiaYiKOboo3FOmAMMFB6mQh2VxDLbVkln1CRXdZpOVksaH+tcOECo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714489160; c=relaxed/simple; bh=Wx8nv2RbfUU5/rQlE3i/mjUA8iZwQsBV38GWN5zfJWQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=a5TBhF76ADcTho4Qj0uvnMTOAYbSfbtepGV49gJ/DrWACQ9fW3J0Ef5QS/wS97JHSdGgiFWYocJiueli0pAJJR+B6sGFt/4QCzt4602fbOGuJqYxSzLstv9M9uflvas63UNhhhPAZ/bWrOCX+9L+hdmjlE4v8icZNp57XhGYJn8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=lxh8ISrb; arc=none smtp.client-ip=198.175.65.19 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="lxh8ISrb" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1714489159; x=1746025159; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Wx8nv2RbfUU5/rQlE3i/mjUA8iZwQsBV38GWN5zfJWQ=; b=lxh8ISrbHrV3PAk85TUTl/vq/MwKQxAd3Z+q+eezvedhbrIbwNla+Q+M 2iM+j3dCiXMDp8G5XxaL6ydqe3f+73sNuadcaBT0bENiruR2o39bYmlH/ 1rb7svRymh2bCYKa1RtWCUeWPVuZRn0o3nInfh235WZZ9vuDXoG/ZSTWp 5IKqvM8Z+0+9Tk1MSDMe5/sqZPDvQvze8dDiiAEil/1pc+hBJP8nCUxf2 XBwqBcnx5DhBTVDzrwKNrsRBrt3z0o/qufQ1PpA8nO2svGsG+kU1/4Yfu elEx5lvPfua/BYAKAEY5jGFo6uIMhD+AEaDXxXbRX2muRkoG8fW+7MaWB Q==; X-CSE-ConnectionGUID: Zdvurq0SQ9CsIrUT3gJR4w== X-CSE-MsgGUID: 2PCizF9bTOuLkSp2WpOoEw== X-IronPort-AV: E=McAfee;i="6600,9927,11060"; a="10059968" X-IronPort-AV: E=Sophos;i="6.07,242,1708416000"; d="scan'208";a="10059968" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa111.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Apr 2024 07:59:19 -0700 X-CSE-ConnectionGUID: GjqcLLnXRveNymwkeudbPQ== X-CSE-MsgGUID: Qh/svWXxTzucCo+BYzDylA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,242,1708416000"; d="scan'208";a="31282246" Received: from unknown (HELO allen-box.sh.intel.com) ([10.239.159.127]) by orviesa004.jf.intel.com with ESMTP; 30 Apr 2024 07:59:15 -0700 From: Lu Baolu To: Jason Gunthorpe , Kevin Tian , Joerg Roedel , Will Deacon , Robin Murphy , Jean-Philippe Brucker , Nicolin Chen , Yi Liu , Jacob Pan , Joel Granados Cc: iommu@lists.linux.dev, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Lu Baolu Subject: [PATCH v5 8/9] iommufd/selftest: Add IOPF support for mock device Date: Tue, 30 Apr 2024 22:57:09 +0800 Message-Id: <20240430145710.68112-9-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240430145710.68112-1-baolu.lu@linux.intel.com> References: <20240430145710.68112-1-baolu.lu@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Extend the selftest mock device to support generating and responding to an IOPF. Also add an ioctl interface to userspace applications to trigger the IOPF on the mock device. This would allow userspace applications to test the IOMMUFD's handling of IOPFs without having to rely on any real hardware. Signed-off-by: Lu Baolu --- drivers/iommu/iommufd/iommufd_test.h | 8 ++++ drivers/iommu/iommufd/selftest.c | 63 ++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/drivers/iommu/iommufd/iommufd_test.h b/drivers/iommu/iommufd/i= ommufd_test.h index e854d3f67205..acbbba1c6671 100644 --- a/drivers/iommu/iommufd/iommufd_test.h +++ b/drivers/iommu/iommufd/iommufd_test.h @@ -22,6 +22,7 @@ enum { IOMMU_TEST_OP_MOCK_DOMAIN_FLAGS, IOMMU_TEST_OP_DIRTY, IOMMU_TEST_OP_MD_CHECK_IOTLB, + IOMMU_TEST_OP_TRIGGER_IOPF, }; =20 enum { @@ -127,6 +128,13 @@ struct iommu_test_cmd { __u32 id; __u32 iotlb; } check_iotlb; + struct { + __u32 dev_id; + __u32 pasid; + __u32 grpid; + __u32 perm; + __u64 addr; + } trigger_iopf; }; __u32 last; }; diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selft= est.c index 97ce62602e66..f925f5e5c00a 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -504,6 +504,8 @@ static bool mock_domain_capable(struct device *dev, enu= m iommu_cap cap) return false; } =20 +static struct iopf_queue *mock_iommu_iopf_queue; + static struct iommu_device mock_iommu_device =3D { }; =20 @@ -514,6 +516,29 @@ static struct iommu_device *mock_probe_device(struct d= evice *dev) return &mock_iommu_device; } =20 +static void mock_domain_page_response(struct device *dev, struct iopf_faul= t *evt, + struct iommu_page_response *msg) +{ +} + +static int mock_dev_enable_feat(struct device *dev, enum iommu_dev_feature= s feat) +{ + if (feat !=3D IOMMU_DEV_FEAT_IOPF || !mock_iommu_iopf_queue) + return -ENODEV; + + return iopf_queue_add_device(mock_iommu_iopf_queue, dev); +} + +static int mock_dev_disable_feat(struct device *dev, enum iommu_dev_featur= es feat) +{ + if (feat !=3D IOMMU_DEV_FEAT_IOPF || !mock_iommu_iopf_queue) + return -ENODEV; + + iopf_queue_remove_device(mock_iommu_iopf_queue, dev); + + return 0; +} + static const struct iommu_ops mock_ops =3D { /* * IOMMU_DOMAIN_BLOCKED cannot be returned from def_domain_type() @@ -529,6 +554,9 @@ static const struct iommu_ops mock_ops =3D { .capable =3D mock_domain_capable, .device_group =3D generic_device_group, .probe_device =3D mock_probe_device, + .page_response =3D mock_domain_page_response, + .dev_enable_feat =3D mock_dev_enable_feat, + .dev_disable_feat =3D mock_dev_disable_feat, .default_domain_ops =3D &(struct iommu_domain_ops){ .free =3D mock_domain_free, @@ -1397,6 +1425,31 @@ static int iommufd_test_dirty(struct iommufd_ucmd *u= cmd, unsigned int mockpt_id, return rc; } =20 +static int iommufd_test_trigger_iopf(struct iommufd_ucmd *ucmd, + struct iommu_test_cmd *cmd) +{ + struct iopf_fault event =3D { }; + struct iommufd_device *idev; + + idev =3D iommufd_get_device(ucmd, cmd->trigger_iopf.dev_id); + if (IS_ERR(idev)) + return PTR_ERR(idev); + + event.fault.prm.flags =3D IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE; + if (cmd->trigger_iopf.pasid !=3D IOMMU_NO_PASID) + event.fault.prm.flags |=3D IOMMU_FAULT_PAGE_REQUEST_PASID_VALID; + event.fault.type =3D IOMMU_FAULT_PAGE_REQ; + event.fault.prm.addr =3D cmd->trigger_iopf.addr; + event.fault.prm.pasid =3D cmd->trigger_iopf.pasid; + event.fault.prm.grpid =3D cmd->trigger_iopf.grpid; + event.fault.prm.perm =3D cmd->trigger_iopf.perm; + + iommu_report_device_fault(idev->dev, &event); + iommufd_put_object(ucmd->ictx, &idev->obj); + + return 0; +} + void iommufd_selftest_destroy(struct iommufd_object *obj) { struct selftest_obj *sobj =3D container_of(obj, struct selftest_obj, obj); @@ -1472,6 +1525,8 @@ int iommufd_test(struct iommufd_ucmd *ucmd) cmd->dirty.page_size, u64_to_user_ptr(cmd->dirty.uptr), cmd->dirty.flags); + case IOMMU_TEST_OP_TRIGGER_IOPF: + return iommufd_test_trigger_iopf(ucmd, cmd); default: return -EOPNOTSUPP; } @@ -1513,6 +1568,9 @@ int __init iommufd_test_init(void) &iommufd_mock_bus_type.nb); if (rc) goto err_sysfs; + + mock_iommu_iopf_queue =3D iopf_queue_alloc("mock-iopfq"); + return 0; =20 err_sysfs: @@ -1528,6 +1586,11 @@ int __init iommufd_test_init(void) =20 void iommufd_test_exit(void) { + if (mock_iommu_iopf_queue) { + iopf_queue_free(mock_iommu_iopf_queue); + mock_iommu_iopf_queue =3D NULL; + } + iommu_device_sysfs_remove(&mock_iommu_device); iommu_device_unregister_bus(&mock_iommu_device, &iommufd_mock_bus_type.bus, --=20 2.34.1