From patchwork Mon Jan 22 07:39:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolu Lu X-Patchwork-Id: 190081 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2bc4:b0:101:a8e8:374 with SMTP id hx4csp2501403dyb; Mon, 22 Jan 2024 03:17:40 -0800 (PST) X-Google-Smtp-Source: AGHT+IEgpVmQC6rsEwJpVbwgGu78v7k8wynFoim0YnArcuvRsDEplsAlw2koBve60xpaOQZyewsx X-Received: by 2002:a05:6358:7a9:b0:176:25c7:803e with SMTP id n41-20020a05635807a900b0017625c7803emr1824811rwj.5.1705922260415; Mon, 22 Jan 2024 03:17:40 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705922260; cv=pass; d=google.com; s=arc-20160816; b=xyRqZjm2SZIDUsTigKTYjmeJccteEo+h9oT6rRvxbg1bwNnV2A79mISrkrX5Wbf0DL nat/byLzAsD3zaYhi4hlAbPS5jBmPVToguC4kO/NknqoBg+FiownK0TDXWn4e457mS+8 Fukz4xCsmB3bu99ljX7EKpSILabdbdZMw2B0owfQsFigawtsvWJ4d5wwY74inoGy836u ObtjhFXD2uzXCxavoKBoNLGkb2qJ0DDrjNOUFnib3dTB9DYPYnKC+dtUC3K/RdVeeky2 +PG3NZrIDRmMiNZfcpbhUwT63jh5xsh8ENhQJl0CNWVKmx/5AU6N8wqADSqfX2aCBXLX ZevQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=e8C6+14LarboSqJ/X/M378QZi3zGhMGGtBFHWA9qj9M=; fh=tXslIEC/iII+0o9YvlUtRXapl6zSPusBr1yEidN1Q7w=; b=OpMg0Iu0EhopT36RPnSYjBNlutlQpLz67aEBI9BxQ4kZ+YoJUwyTqL/UT2vv/ZF5GG eq9+5L/ygLkAxNtMG7L5qTCsnUi4Gsr13quOjEyRGBlTiQXHeZTDO64IsGoJAPAqjWYg 03v+7NwyCVB0R1tFwXTvunz+ThQjacMjLcDRUxwZoojH2KYERVd0OSoqSIyoqJA0F+0I 4Y8yehE7INJn8o7zH+HqwiBMAqU76+gJs4sRtyxSM094EpDv9vlvommi3aJlFw82F6Es LRrF//HwMOwP5n3uVbFUEPFuyJqqIGYgB1y/40f5qX3Pd95eScsdnuTvGSQblMYwJ7kl SNwQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=i3OB1Vui; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-32459-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-32459-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id q2-20020a63f942000000b005cec90b9a98si7945681pgk.835.2024.01.22.03.17.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jan 2024 03:17:40 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-32459-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=i3OB1Vui; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-32459-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-32459-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 91957B26037 for ; Mon, 22 Jan 2024 07:47:18 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9271638FAA; Mon, 22 Jan 2024 07:45:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="i3OB1Vui" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) (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 27F713838E for ; Mon, 22 Jan 2024 07:45:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705909503; cv=none; b=sARvoc2eCLbaIfv+98KtTqqgsdpG+N2ECiIMz6MX59aalJ3FD6SuIfzlOYFyElAIJWQY/AoMFU8KOF0bShmiJUQmrmlb2h0UTc5PEcrl4q4nmlpodPgOTN1Q9cazvgo2iEzTgc5BZ/dzK3lVeX6kTQ0+9JE46PYNLtoVYEk9+1w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705909503; c=relaxed/simple; bh=DwSOH+5N741uvq3khDnkaUrB3woK0aLhmUoTQu04tCc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Pwz3EO6aPI5J5V2BntFci1KKjQ8251rpTS7mFp7fJ/xmHwSxEB9d6wyaNKX3LbETtaSLcWSQy/+JTmUpJrTuGV0pBM8c5EFAAlB+pTfKqWzEHZq97iC2atqCtgdh2gdNmcgbzdHeY2vqWOBl3aUZ/3LX+qewDMv+PqDdVXacSPE= 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=i3OB1Vui; arc=none smtp.client-ip=192.198.163.7 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1705909502; x=1737445502; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=DwSOH+5N741uvq3khDnkaUrB3woK0aLhmUoTQu04tCc=; b=i3OB1VuiZl/yRxSNnez0A1nZrEyX918Gw52m8yw77P6PiM7ImaW3NUu1 +DQDcm6+wypPZ/C1TvKlc1dlMq1N7mZbvDizPVsRTr122ikzxT/+uOu8c A4cyRS64ojrV9OcgxPe29FCAd63LySgxKtZTsYLyzcLK23F1NoKkG1d16 HunfNU7nRCvcgQRnnctXizL3g4d9iMXmTvs4S2katbZwgJGumtdQOW+XN zRD9eZoMA3i5MBLXTmjfM6cV7Mh3FJ0ytjq9d2a+lZKXmEqPge3e3aWMk Z4bk+8rc38H3oYwBh3qbiR85YqeoXhAkYlM5Ew1v8FFhi6H10RFCQJQ/1 w==; X-IronPort-AV: E=McAfee;i="6600,9927,10960"; a="22611660" X-IronPort-AV: E=Sophos;i="6.05,211,1701158400"; d="scan'208";a="22611660" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Jan 2024 23:45:01 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10960"; a="778505044" X-IronPort-AV: E=Sophos;i="6.05,211,1701158400"; d="scan'208";a="778505044" Received: from allen-box.sh.intel.com ([10.239.159.127]) by orsmga007.jf.intel.com with ESMTP; 21 Jan 2024 23:44:57 -0800 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 v3 7/8] iommufd/selftest: Add IOPF support for mock device Date: Mon, 22 Jan 2024 15:39:02 +0800 Message-Id: <20240122073903.24406-8-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240122073903.24406-1-baolu.lu@linux.intel.com> References: <20240122073903.24406-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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1788789140013551134 X-GMAIL-MSGID: 1788789140013551134 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/iommufd_test.h index 482d4059f5db..ff9dcd812618 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, }; enum { @@ -126,6 +127,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/selftest.c index 2fb2597e069f..2ca226f88856 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -445,6 +445,8 @@ static bool mock_domain_capable(struct device *dev, enum iommu_cap cap) return false; } +static struct iopf_queue *mock_iommu_iopf_queue; + static struct iommu_device mock_iommu_device = { }; @@ -455,6 +457,29 @@ static struct iommu_device *mock_probe_device(struct device *dev) return &mock_iommu_device; } +static void mock_domain_page_response(struct device *dev, struct iopf_fault *evt, + struct iommu_page_response *msg) +{ +} + +static int mock_dev_enable_feat(struct device *dev, enum iommu_dev_features feat) +{ + if (feat != 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_features feat) +{ + if (feat != 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 = { /* * IOMMU_DOMAIN_BLOCKED cannot be returned from def_domain_type() @@ -470,6 +495,9 @@ static const struct iommu_ops mock_ops = { .capable = mock_domain_capable, .device_group = generic_device_group, .probe_device = mock_probe_device, + .page_response = mock_domain_page_response, + .dev_enable_feat = mock_dev_enable_feat, + .dev_disable_feat = mock_dev_disable_feat, .default_domain_ops = &(struct iommu_domain_ops){ .free = mock_domain_free, @@ -1333,6 +1361,31 @@ static int iommufd_test_dirty(struct iommufd_ucmd *ucmd, unsigned int mockpt_id, return rc; } +static int iommufd_test_trigger_iopf(struct iommufd_ucmd *ucmd, + struct iommu_test_cmd *cmd) +{ + struct iopf_fault event = { }; + struct iommufd_device *idev; + + 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; + + 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 = container_of(obj, struct selftest_obj, obj); @@ -1408,6 +1461,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; } @@ -1449,6 +1504,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: @@ -1464,6 +1522,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,