From patchwork Thu Oct 26 02:49:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolu Lu X-Patchwork-Id: 158366 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:d641:0:b0:403:3b70:6f57 with SMTP id cy1csp386346vqb; Wed, 25 Oct 2023 19:54:58 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGqbdyjwQ0sEijg0F6ge9A6R2TTP9cOzBH5Vatw7ScYJyNLORkw+3yhv8MClBJw4bU9iREq X-Received: by 2002:a05:620a:8f16:b0:778:5dc:fb74 with SMTP id rh22-20020a05620a8f1600b0077805dcfb74mr15107977qkn.68.1698288898121; Wed, 25 Oct 2023 19:54:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698288898; cv=none; d=google.com; s=arc-20160816; b=RJpQlhhDBgKmsnWnnoEeumslxncplpoa9dpuNlIP3zxraUdJRF6RjoJdL9YNuryI9M 5kST6AX2HppjncPlKLga/8CIJgHmgc1tpr6LX6Q5tloz1j1z+loKbYYw/PI2Wozrc8MZ I6Q7wCwJGAAZgA17f9j4hnVbXeh17A9OKUFNCJNreOLDcUXvkwcgRRXS/cQetTqVzRWF aiUnluMIiyLzdGiA3yH2lJmlRP5d5ica5FMVqR/figu6V/Si/DZo10x8/E2rGDzthXzQ srDRocsPDL7+0TBBY6wJAKrK0bPFmueT5Wt2/pQjDESrvhw4jTQP7cZ+Kowp/4hPAyXj p3eA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=67xYP3mD9Qa1srOzAr2r5DKmyV+qtWBL+paYuScTo60=; fh=6/CvhEMXy459ByfFdUERwF43WFRGrwvwfaBeFRnf1YE=; b=aFCrWZ3t2+QA7UNAN90zQnRwCX4EF0jzg9yW7rCML9yavKIWcE2aXmbunPb/ynPTHB cupdUYe8q3sdK+sDqNcEom3vMXMS9nibZXY6DieCRNgrAeTREND58Cw2GnLwUz+vdr47 3CTepwJVwCzEPDutJJozdKx8pdKySo7DLbnlBVmAfICQagtvA+CKb3pAUoFSTm4fViuv oDtQdqMdKPrU4ceP0XewnKKWnzVmHbqfCedhiI2IaPU0vBV+jBgLYwnpl3B8ZWuqghxv 9Ji27kNwhURdIXoY09lrZ5obCG1p/zGISVrOtyZXwUv5tRk6vE1a2+CbOGLP3FA2MVm5 Tmyw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=VquIDyw+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id 14-20020a25030e000000b00da03c34c384si6286740ybd.24.2023.10.25.19.54.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Oct 2023 19:54:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=VquIDyw+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 5DD348028BBB; Wed, 25 Oct 2023 19:54:55 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233179AbjJZCyK (ORCPT + 25 others); Wed, 25 Oct 2023 22:54:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60218 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234684AbjJZCyA (ORCPT ); Wed, 25 Oct 2023 22:54:00 -0400 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F1DFDD48; Wed, 25 Oct 2023 19:53:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1698288835; x=1729824835; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Y/UELdAKdixD7UAeztRKsYvoefrXj6uhYDxNLPIhGxs=; b=VquIDyw+GABnBgSiZP0rQGLB4wNXsfsV2PSgvkQDQz+NDTppJO8+8geQ dZo+rJcdIsT0r0/VjeSqyRGP3fmWOMm1dr2fPBZXVAe7AZAHVnqfd2omG p2s0Lo130ooOUNRz0Y/0d+HSlTIno8bQoIp7r6kG511B3JWVwjEyJg3EY c3gI2jkB31QZj7xmCOtAwl6kDHunQy4Wej7gAqFyp3RhysnOqDWZlfgAw F2ou/c3dvwblA0Q1aASY2fw1xqDJx4HynTCVTD0IuB1AtiqxZkEIpYcBy Bz9PlXONZbPUSIE81+FfVhBLpVv3UoH8oRUH37k5outTAVrFC7L0KEOAc w==; X-IronPort-AV: E=McAfee;i="6600,9927,10874"; a="391316202" X-IronPort-AV: E=Sophos;i="6.03,252,1694761200"; d="scan'208";a="391316202" Received: from orsmga006.jf.intel.com ([10.7.209.51]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Oct 2023 19:53:54 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10874"; a="735604559" X-IronPort-AV: E=Sophos;i="6.03,252,1694761200"; d="scan'208";a="735604559" Received: from allen-box.sh.intel.com ([10.239.159.127]) by orsmga006.jf.intel.com with ESMTP; 25 Oct 2023 19:53:50 -0700 From: Lu Baolu To: Jason Gunthorpe , Kevin Tian , Joerg Roedel , Will Deacon , Robin Murphy , Jean-Philippe Brucker , Nicolin Chen , Yi Liu , Jacob Pan Cc: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Lu Baolu Subject: [PATCH v2 5/6] iommufd/selftest: Add IOMMU_TEST_OP_TRIGGER_IOPF test support Date: Thu, 26 Oct 2023 10:49:29 +0800 Message-Id: <20231026024930.382898-6-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231026024930.382898-1-baolu.lu@linux.intel.com> References: <20231026024930.382898-1-baolu.lu@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,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 groat.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 (groat.vger.email [0.0.0.0]); Wed, 25 Oct 2023 19:54:55 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780784979643036205 X-GMAIL-MSGID: 1780784979643036205 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 | 56 ++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/drivers/iommu/iommufd/iommufd_test.h b/drivers/iommu/iommufd/iommufd_test.h index 65a363f5e81e..98951a2af4bd 100644 --- a/drivers/iommu/iommufd/iommufd_test.h +++ b/drivers/iommu/iommufd/iommufd_test.h @@ -21,6 +21,7 @@ enum { IOMMU_TEST_OP_ACCESS_REPLACE_IOAS, IOMMU_TEST_OP_MD_CHECK_IOTLB, IOMMU_TEST_OP_DEV_CHECK_DATA, + IOMMU_TEST_OP_TRIGGER_IOPF, }; enum { @@ -109,6 +110,13 @@ struct iommu_test_cmd { struct { __u32 val; } check_dev_data; + 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/selftest.c index 117776d236dc..b2d2edc3d2d2 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -401,11 +401,21 @@ static void mock_domain_set_plaform_dma_ops(struct device *dev) */ } +static struct iopf_queue *mock_iommu_iopf_queue; + static struct iommu_device mock_iommu_device = { }; static struct iommu_device *mock_probe_device(struct device *dev) { + int rc; + + if (mock_iommu_iopf_queue) { + rc = iopf_queue_add_device(mock_iommu_iopf_queue, dev); + if (rc) + return ERR_PTR(-ENODEV); + } + return &mock_iommu_device; } @@ -431,6 +441,12 @@ static void mock_domain_unset_dev_user_data(struct device *dev) mdev->dev_data = 0; } +static int mock_domain_page_response(struct device *dev, struct iopf_fault *evt, + struct iommu_page_response *msg) +{ + return 0; +} + static const struct iommu_ops mock_ops = { .owner = THIS_MODULE, .pgsize_bitmap = MOCK_IO_PAGE_SIZE, @@ -443,6 +459,7 @@ static const struct iommu_ops mock_ops = { .probe_device = mock_probe_device, .set_dev_user_data = mock_domain_set_dev_user_data, .unset_dev_user_data = mock_domain_unset_dev_user_data, + .page_response = mock_domain_page_response, .default_domain_ops = &(struct iommu_domain_ops){ .free = mock_domain_free, @@ -542,6 +559,9 @@ static void mock_dev_release(struct device *dev) { struct mock_dev *mdev = container_of(dev, struct mock_dev, dev); + if (mock_iommu_iopf_queue) + iopf_queue_remove_device(mock_iommu_iopf_queue, dev); + atomic_dec(&mock_dev_num); kfree(mdev); } @@ -1200,6 +1220,32 @@ static_assert((unsigned int)MOCK_ACCESS_RW_WRITE == IOMMUFD_ACCESS_RW_WRITE); static_assert((unsigned int)MOCK_ACCESS_RW_SLOW_PATH == __IOMMUFD_ACCESS_RW_SLOW_PATH); +static int iommufd_test_trigger_iopf(struct iommufd_ucmd *ucmd, + struct iommu_test_cmd *cmd) +{ + struct iopf_fault event = { }; + struct iommufd_device *idev; + int rc; + + idev = iommufd_get_device(ucmd, cmd->trigger_iopf.dev_id); + if (IS_ERR(idev)) + return PTR_ERR(idev); + + event.fault.prm.flags = IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE; + if (cmd->trigger_iopf.pasid != IOMMU_NO_PASID) + event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_PASID_VALID; + event.fault.type = IOMMU_FAULT_PAGE_REQ; + event.fault.prm.addr = cmd->trigger_iopf.addr; + event.fault.prm.pasid = cmd->trigger_iopf.pasid; + event.fault.prm.grpid = cmd->trigger_iopf.grpid; + event.fault.prm.perm = cmd->trigger_iopf.perm; + + rc = iommu_report_device_fault(idev->dev, &event); + iommufd_put_object(&idev->obj); + + return rc; +} + void iommufd_selftest_destroy(struct iommufd_object *obj) { struct selftest_obj *sobj = container_of(obj, struct selftest_obj, obj); @@ -1271,6 +1317,8 @@ int iommufd_test(struct iommufd_ucmd *ucmd) return -EINVAL; iommufd_test_memory_limit = cmd->memory_limit.limit; return 0; + case IOMMU_TEST_OP_TRIGGER_IOPF: + return iommufd_test_trigger_iopf(ucmd, cmd); default: return -EOPNOTSUPP; } @@ -1312,6 +1360,9 @@ int __init iommufd_test_init(void) &iommufd_mock_bus_type.nb); if (rc) goto err_sysfs; + + mock_iommu_iopf_queue = iopf_queue_alloc("mock-iopfq"); + return 0; err_sysfs: @@ -1327,6 +1378,11 @@ int __init iommufd_test_init(void) void iommufd_test_exit(void) { + if (mock_iommu_iopf_queue) { + iopf_queue_free(mock_iommu_iopf_queue); + mock_iommu_iopf_queue = NULL; + } + iommu_device_sysfs_remove(&mock_iommu_device); iommu_device_unregister_bus(&mock_iommu_device, &iommufd_mock_bus_type.bus,