From patchwork Thu May 11 14:38:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 92678 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp4435918vqo; Thu, 11 May 2023 07:54:18 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ594kEl42So0C3HpX/w7FWThWr1/exsCqX6aBkI5uq0Ar3i5t6akEJShoU5FPGYc2Ac42Gi X-Received: by 2002:a05:6a20:6a0f:b0:102:f6f2:c962 with SMTP id p15-20020a056a206a0f00b00102f6f2c962mr7478866pzk.54.1683816858423; Thu, 11 May 2023 07:54:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683816858; cv=none; d=google.com; s=arc-20160816; b=vsW6tSTzh9RPi5W+UhYptzAjXIJszPcL5gKcg6uHXsI+MoIUKxHZqBZZYkS1KM6l/B WH8S2BZfOUZRT3X3daIduzBVLgsoJeDACzER0+45TptP8PGp/UY7VIL7JFJh2U7BwZFy 3PUBKfovL/DXBxf8wDYYSP2Y5CD9h3kxX4bRiYR4vyWD+iEg3Lfeh9TZSgZmrNGUpguo 3eumj5gazXvWkIPG8KHmvc2RCmP69fcRnefsV42YGudC8A2Iww9KdeC5P5eMQX17aICD 8a36yVsy0MxeefL2+/2tFsM0VhicSg4vA0+uIxaB6dfDQWsdSFYvtS332e4b/U7DJFhr 8XJw== 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=vs6mgI929VPcsZbNcxkZqesJSbo74Vl48nTHuNVeL/k=; b=mwV9JO56kj0SRa3D1ujQjUS6DwBzU7xUBctnWVprxcSOxVH+Ziek0yhLCqkfVjYGvU 6coZrnOByWBQI/EW7/asqC1BRd7IQRel1JW0i4K8ZHjQHJL0b/dTFigOULXSP+xP73dz 6OoqtK2APoScTgEdiSABX8ZAAId8DzuhHEfuY9Ks4d+KkqVIdlxilXFmcy7g/o5KcH2W E07pybQBFomejbPEHYE/vEUCYTyQz6g5BJ87Z9jhFtbgn6VSqMMirVKJiznDLoL0HOxX ABV3BFtyEITdPhe0yfme1LbbeTGNHh7x3cxGWjKyD2OJHfr7EiL36KmzNdkr3q12m3BY +kzQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=IK8cRKaV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y17-20020a63b511000000b005031abe8d8bsi1665684pge.745.2023.05.11.07.54.02; Thu, 11 May 2023 07:54:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=IK8cRKaV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238638AbjEKOm4 (ORCPT + 99 others); Thu, 11 May 2023 10:42:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35908 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238340AbjEKOma (ORCPT ); Thu, 11 May 2023 10:42:30 -0400 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1391310E42; Thu, 11 May 2023 07:38:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1683815928; x=1715351928; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=LthEg6EGU/MY/viwuO2PF5RAMF9yzjhBqwVfgZnbeuQ=; b=IK8cRKaVh986bbPDLgLOiqOcYBFSdFbX4ZAS1EKLOpODF9FCS9R1T+a7 ug01Tv7xuj68qxoHBd94+HJI4TDI/Xo+98Wz5EnnzrsdR+7KUXdcQRH/l ZCYedpXJzqwT9LQMdK9rBMBtNC5hm1hwgPUefe1wBL1xRKpLXYF5GHPCd 0k7GoaFnd7HHBYtv2gzFwpRmiLJajBCy6CXG3xxjUwqfUNP10Z0xhNd9N YYQhElV3/rBY7Xq50IyP6vuOpv0lLjnU/eSuFRTOLMDQZrLx1vqfIoJZF PZDDkC2QB+qFh4K2SYiQtPyrhOKkHvmKsUCKT282/Mxp2BKM9OHsm3Uk3 w==; X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="339812846" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="339812846" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 May 2023 07:38:47 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="730382596" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="730382596" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by orsmga008.jf.intel.com with ESMTP; 11 May 2023 07:38:47 -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 Subject: [PATCH v2 01/11] iommu: Add new iommu op to create domains owned by userspace Date: Thu, 11 May 2023 07:38:34 -0700 Message-Id: <20230511143844.22693-2-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230511143844.22693-1-yi.l.liu@intel.com> References: <20230511143844.22693-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765609945903891522?= X-GMAIL-MSGID: =?utf-8?q?1765609945903891522?= From: Lu Baolu Introduce a new iommu_domain op to create domains owned by userspace, e.g. through iommufd. These domains have a few different properties compares to kernel owned domains: - They may be UNMANAGED domains, but created with special parameters. For instance aperture size changes/number of levels, different IOPTE formats, or other things necessary to make a vIOMMU work - We have to track all the memory allocations with GFP_KERNEL_ACCOUNT to make the cgroup sandbox stronger - Device-specialty domains, such as NESTED domains can be created by iommufd. The new op clearly says the domain is being created by IOMMUFD, that the domain is intended for userspace use, and it provides a way to pass a driver specific uAPI structure to customize the created domain to exactly what the vIOMMU userspace driver requires. iommu drivers that cannot support VFIO/IOMMUFD should not support this op. This includes any driver that cannot provide a fully functional UNMANAGED domain. This op chooses to make the special parameters opaque to the core. This suits the current usage model where accessing any of the IOMMU device special parameters does require a userspace driver that matches the kernel driver. If a need for common parameters, implemented similarly by several drivers, arises then there is room in the design to grow a generic parameter set as well. This new op for now is only supposed to be used by iommufd, hence no wrapper for it. iommufd would call the callback directly. As for domain free, iommufd would use iommu_domain_free(). Also, add an op to return the length of supported user data structures that must be added to include/uapi/include/iommufd.h file. This helps the iommufd core to sanitize the input data before it forwards the data to an iommu driver. Suggested-by: Jason Gunthorpe Signed-off-by: Lu Baolu Co-developed-by: Nicolin Chen Signed-off-by: Nicolin Chen Signed-off-by: Yi Liu --- include/linux/iommu.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/include/linux/iommu.h b/include/linux/iommu.h index a748d60206e7..7f2046fa53a3 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -220,6 +220,15 @@ struct iommu_iotlb_gather { bool queued; }; +/* + * The user data to allocate a specific type user iommu domain + * + * This includes the corresponding driver data structures in + * include/uapi/linux/iommufd.h. + */ +union iommu_domain_user_data { +}; + /** * struct iommu_ops - iommu ops and capabilities * @capable: check capability @@ -229,6 +238,15 @@ struct iommu_iotlb_gather { * after use. Return the data buffer if success, or ERR_PTR on * failure. * @domain_alloc: allocate iommu domain + * @domain_alloc_user: allocate user iommu domain + * @domain_alloc_user_data_len: return the required length of the user data + * to allocate a specific type user iommu domain. + * @hwpt_type is defined as enum iommu_hwpt_type + * in include/uapi/linux/iommufd.h. The returned + * length is the corresponding sizeof driver data + * structures in include/uapi/linux/iommufd.h. + * -EOPNOTSUPP would be returned if the input + * @hwpt_type is not supported by the driver. * @probe_device: Add device to iommu driver handling * @release_device: Remove device from iommu driver handling * @probe_finalize: Do final setup work after the device is added to an IOMMU @@ -269,6 +287,10 @@ struct iommu_ops { /* Domain allocation and freeing by the iommu driver */ struct iommu_domain *(*domain_alloc)(unsigned iommu_domain_type); + struct iommu_domain *(*domain_alloc_user)(struct device *dev, + struct iommu_domain *parent, + const union iommu_domain_user_data *user_data); + int (*domain_alloc_user_data_len)(u32 hwpt_type); struct iommu_device *(*probe_device)(struct device *dev); void (*release_device)(struct device *dev); From patchwork Thu May 11 14:38:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 92675 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp4435342vqo; Thu, 11 May 2023 07:53:24 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5msLl9IFoHImDB7IxA28A3R3LsdR+NWAjsfDEe9hyuw8Paw8i2fV5pcgMl9QNQfxDUQzS3 X-Received: by 2002:a17:902:d48d:b0:1ac:66c4:6071 with SMTP id c13-20020a170902d48d00b001ac66c46071mr22805260plg.57.1683816804605; Thu, 11 May 2023 07:53:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683816804; cv=none; d=google.com; s=arc-20160816; b=KjGNzVki5tQu4J3xxqUJ8Vhz44SaD0NIb/ze9HgGJimyfVAKNsKAtnfPraTIwW63Xo O01zh4OkfUZB2NO+lk9bKhp+HMhLii2prCqROtmalJLqrHi+OKCQ1V7jN9boDqxrxkte cBMNepoor5XETqiMyWFDWgpx1R0mU0bQNiqmiJvHppA5q3K83Nbv/bks6XbqnrJ/YUyv LWk9W21tT6eH8jGCBswZJhK0lPSQOjJnfEEItX33ValG0R7IH0njegOZHAnR7lFVZXTZ cdfXjooNMdeT4d+JeoTdagyieSjV6YHowk0ImdkWQqFHkmDltiEhNZopcmqMmJor32bj pq3Q== 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=ADoxD/cBpgC4HUQpMmzDK6058+DbT8+hdz4b6SgGqjU=; b=ECLfFRLMzNDbMiKlM8pXA8v521Ptx5FkGlrDbexzVPEkweaYaoDPZSAWAxftLr3yPS L5dhfZ4Q6CpLrDmjbxHTKAz5jbsFr/Xkk7uopIWXqJUuB0Mx2BPFJ0o+OP6ciO2Pre0/ L6dNOunCxVG3aC6TKI27ulX8/zOOsr6ICtqwzqxcLgu25w0c+PmecNZAO0wtlvOhtcFs 9/qmXtkvRhGNz9sk1VFscGddd6UEsWACjaKbXLJIULFE35NaczEymArLshGEQctGzw8k OC+T2skIwmY9kZJ86hckznv4qwFQu23dai3adr1TA/Tv8EdOVIb924hBSvMmKjn+SOe+ y+eQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=UHHZukxg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k7-20020a170902c40700b001a1cefdaa33si7156152plk.342.2023.05.11.07.53.11; Thu, 11 May 2023 07:53:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=UHHZukxg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238682AbjEKOnI (ORCPT + 99 others); Thu, 11 May 2023 10:43:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35918 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238612AbjEKOma (ORCPT ); Thu, 11 May 2023 10:42:30 -0400 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 844111249E; Thu, 11 May 2023 07:38:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1683815929; x=1715351929; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=4spnTWVSxrPcfUADcyFVe2yRLnmd/04snICB4zgJN08=; b=UHHZukxg4GvTdUS9dMJxWijmy8E6dmZITQe/5ZFi+PzocrV/txmny/TZ jhGF2Zb2UMNvnf1yGmc1BgqrANOKl0dlmJ1lmziyIRzwhCIlW7I+bLCSZ 16HjJJRWERUMgC4TdmfpVAb7EuCmHwibhseNRCT64fnssb14ryztqKih4 tbpd+qi1W/xOn76GUT9tBYCiynk+lkO2j+053w8MltIL9QilaEET6hOxN vt4HT/v5VaYoik9oz6vdmXXJRbXV0xsq7PiUwve70YMuTYxqWr00IYfef yJremhwpPClBFbU2XqRK8desiuF42izZ3BpHxJ4HqlhT3GuySnMY0j07p w==; X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="339812861" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="339812861" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 May 2023 07:38:48 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="730382603" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="730382603" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by orsmga008.jf.intel.com with ESMTP; 11 May 2023 07:38:47 -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 Subject: [PATCH v2 02/11] iommu: Add nested domain support Date: Thu, 11 May 2023 07:38:35 -0700 Message-Id: <20230511143844.22693-3-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230511143844.22693-1-yi.l.liu@intel.com> References: <20230511143844.22693-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765609889866181609?= X-GMAIL-MSGID: =?utf-8?q?1765609889866181609?= From: Lu Baolu Introduce a new domain type for a user I/O page table, which is nested on top of another user space address represented by a UNMANAGED domain. The mappings of a nested domain are managed by user space software, therefore it's unnecessary to have map/unmap callbacks. But the updates of the PTEs in the nested domain page table must be propagated to the caches on both IOMMU (IOTLB) and devices (DevTLB). The nested domain is allocated by the domain_alloc_user op, and attached to the device through the existing iommu_attach_device/group() interfaces. A new domain op, named cache_invalidate_user is added for the userspace to flush the hardware caches for a nested domain through iommufd. No wrapper for it, as it's only supposed to be used by iommufd. Signed-off-by: Lu Baolu Co-developed-by: Nicolin Chen Signed-off-by: Nicolin Chen Signed-off-by: Yi Liu --- include/linux/iommu.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 7f2046fa53a3..c2d0fa3e2e18 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -66,6 +66,9 @@ struct iommu_domain_geometry { #define __IOMMU_DOMAIN_SVA (1U << 4) /* Shared process address space */ +#define __IOMMU_DOMAIN_NESTED (1U << 5) /* User-managed IOVA nested on + a stage-2 translation */ + /* * This are the possible domain-types * @@ -91,6 +94,7 @@ struct iommu_domain_geometry { __IOMMU_DOMAIN_DMA_API | \ __IOMMU_DOMAIN_DMA_FQ) #define IOMMU_DOMAIN_SVA (__IOMMU_DOMAIN_SVA) +#define IOMMU_DOMAIN_NESTED (__IOMMU_DOMAIN_NESTED) struct iommu_domain { unsigned type; @@ -346,6 +350,10 @@ struct iommu_ops { * @iotlb_sync_map: Sync mappings created recently using @map to the hardware * @iotlb_sync: Flush all queued ranges from the hardware TLBs and empty flush * queue + * @cache_invalidate_user: Flush hardware TLBs caching user space IO mappings + * @cache_invalidate_user_data_len: Defined length of input user data for the + * cache_invalidate_user op, being sizeof the + * structure in include/uapi/linux/iommufd.h * @iova_to_phys: translate iova to physical address * @enforce_cache_coherency: Prevent any kind of DMA from bypassing IOMMU_CACHE, * including no-snoop TLPs on PCIe or other platform @@ -375,6 +383,9 @@ struct iommu_domain_ops { size_t size); void (*iotlb_sync)(struct iommu_domain *domain, struct iommu_iotlb_gather *iotlb_gather); + int (*cache_invalidate_user)(struct iommu_domain *domain, + void *user_data); + const size_t cache_invalidate_user_data_len; phys_addr_t (*iova_to_phys)(struct iommu_domain *domain, dma_addr_t iova); From patchwork Thu May 11 14:38:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 92676 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp4435453vqo; Thu, 11 May 2023 07:53:38 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7jAb1lDektWG60kk9vfPScZFkQRbbhgbkgV8egxe4NBIbNL22KzAbEONWjV/WPUOFnmwc2 X-Received: by 2002:a05:6a20:1587:b0:100:af1c:f6b4 with SMTP id h7-20020a056a20158700b00100af1cf6b4mr18395839pzj.41.1683816817941; Thu, 11 May 2023 07:53:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683816817; cv=none; d=google.com; s=arc-20160816; b=Tc4AIBZjkpQif3GfGuCs1hqOUabqE+8Y0StH1q2HpsjBc5gIIorCByfkjTOe8Fn6lQ eyjO/aM8+yxGLA9PS5q+9G0GInpMG0A/ZglFAMjmhM9D5b1TSiPTVAvzQApEYALgDDxo 3nhC6Ucs2DT7R/CRYeIHCgxSgmArideKu9/py6b9kI/M+D0ackT45OR1N9pfyNAJSkLX nEZ/2QX+U7+CxZJRDm7gY/lUgd3iQbYP0bJZiuzxmx115GLlIMCFzJXg4YFoJfot8IKJ i5uwxpR9Nk6ViOBfT1NrgzYLR7zvwfhJFx5wnnp36OGdifnO9m428qBj2zyEG0khJcRx QxqA== 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=GbgkZFz841rSMMYOaQo4zWSCHCqEA9H2kr+Uq45uhjk=; b=k1D7rrx7VW2TtGj+d3ludLRjEgxvNLvyCUYho+hDZ6dHM3RxUsiQvAEIUtpo0rZWzh qH1mCE534cWrGDnGdP/V58wk64ZfsBijexnqJJUqUpod7d4ZWlEWFZnX0Jnc50UYYHlY bRl3Ae5lG/WRFGvB6ROx2btSBBBnuZty+DPfbfUbb6CIoYf4eB0y5hBo22U7uQnuJetr gcM1mzo3juxok6VPo4o+zIKBku3jm3RLsJSR+Uwz+LeogqX1agJzsd+sEan+Cmqb+rbE RwkdwIab4fsN8pVO1A5gbWgoCbLceJpfPdSeQ4ApLDW6hf/It7fJHubr7TFiYgAAxlO8 jS6Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=jM23FZQC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bu17-20020a632951000000b005303d78c47asi6495517pgb.726.2023.05.11.07.53.24; Thu, 11 May 2023 07:53:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=jM23FZQC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238673AbjEKOnE (ORCPT + 99 others); Thu, 11 May 2023 10:43:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35922 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238613AbjEKOma (ORCPT ); Thu, 11 May 2023 10:42:30 -0400 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 962DC124A3; Thu, 11 May 2023 07:38:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1683815930; x=1715351930; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=9oxvupeqb12UC7Ej2KuinDMGVCV9GTwjqOvc7w7xUds=; b=jM23FZQCqnzDBE3hzIwtVtL8xl7m3LulKkmqBSnqLXEFA/0hMwDn7MtI 1yUcnfoJBeti1LFVtc+rHL9QevlwtIQreYO9jc2WqEYjN7uYra09NZLP8 Ed7eidOYNPUaKVZuwnHUBby2st6Nj8znb1Rmpa6VMp37M8LHjybRFxLpO 33Vmpw75YXY2dabfT3cm0dPOivD9yU5PlRvMRIWRkV7D/RrvSTzZcAPzj +QGZ4E/toTQNG8zNpLJ6QehROC63wNJP3MBWjvwh9gSoqvgt6BwcpFh/H ZNVxIRMztlnwm60txxhKas9hAh35tGR/m5YK7c+KVq1pRcLISMl8Xi3nc Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="339812875" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="339812875" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 May 2023 07:38:48 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="730382611" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="730382611" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by orsmga008.jf.intel.com with ESMTP; 11 May 2023 07:38:48 -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 Subject: [PATCH v2 03/11] iommufd/hw_pagetable: Use domain_alloc_user op for domain allocation Date: Thu, 11 May 2023 07:38:36 -0700 Message-Id: <20230511143844.22693-4-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230511143844.22693-1-yi.l.liu@intel.com> References: <20230511143844.22693-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765609903810979533?= X-GMAIL-MSGID: =?utf-8?q?1765609903810979533?= This makes IOMMUFD to use iommu_domain_alloc_user() for iommu_domain creation as IOMMUFD needs to support iommu_domain allocation with parameters from userspace in nesting support. If the iommu driver doesn't provide domain_alloc_user callback then it falls back to use iommu_domain_alloc(). Suggested-by: Jason Gunthorpe Reviewed-by: Lu Baolu Co-developed-by: Nicolin Chen Signed-off-by: Nicolin Chen Signed-off-by: Yi Liu --- drivers/iommu/iommufd/hw_pagetable.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index cf2c1504e20d..b6323ad9c32d 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -5,6 +5,7 @@ #include #include +#include "../iommu-priv.h" #include "iommufd_private.h" void iommufd_hw_pagetable_destroy(struct iommufd_object *obj) @@ -74,6 +75,7 @@ struct iommufd_hw_pagetable * iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, struct iommufd_device *idev, bool immediate_attach) { + const struct iommu_ops *ops = dev_iommu_ops(idev->dev); struct iommufd_hw_pagetable *hwpt; int rc; @@ -88,7 +90,10 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, refcount_inc(&ioas->obj.users); hwpt->ioas = ioas; - hwpt->domain = iommu_domain_alloc(idev->dev->bus); + if (ops->domain_alloc_user) + hwpt->domain = ops->domain_alloc_user(idev->dev, NULL, NULL); + else + hwpt->domain = iommu_domain_alloc(idev->dev->bus); if (!hwpt->domain) { rc = -ENOMEM; goto out_abort; From patchwork Thu May 11 14:38:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 92672 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp4432545vqo; Thu, 11 May 2023 07:49:16 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5tRYtM4zNvb8XwiyWPh6jUTCqyTB7d37oJ4NZpUiNFW8j/q8k5brqV9GAo2GA1g/oPmE7g X-Received: by 2002:a05:6a00:21c9:b0:645:1081:98ec with SMTP id t9-20020a056a0021c900b00645108198ecmr22101878pfj.13.1683816555816; Thu, 11 May 2023 07:49:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683816555; cv=none; d=google.com; s=arc-20160816; b=bB9OAvYK6wQYLz+tvTe3c0iUokj01WZDrXhNjwVJBN8R3SnieR/+sh1krjbVEUpJTO UsC8VSt8ISylso/5xnODxYDJobLkrSysNVMYZwnM3+706RkbTjKjTkc6mIy//rBFI5Qd D9Ei7w4nzGVhLxtzO6UYtnHZh4uAzZAli+gfDgitHgZi17U5M6jFa9uHNeUHO0OWi85e 8JicJfA/WDTxfEgzMYWI7oIymvZ8r/TBWWiCkJ4qp5PTDZtM8FTSfqL+UWm1XFlWw1kK QfsdN/uCgS2eXeEpeiB/Roez07s66jjFlGAapZXkqU4mG5j7MEGVs9HTGWEglXKLUtVq IoKQ== 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=2TECUCEDiaczrX4LkZDczGTei+oN/1bPKdQmLlIezcI=; b=ds3YPFwHlAM3aUWBFejs0Yuoc1+yWPPJOOEUdI8JfB1Nw9BUvjf9d8QJuaQ20Fxv71 o8J4JfJbIzvF1feqiklyU/ibONxZhYIy1IvFTM3XNscxGUi9TZGiEfskPEs1qJiLOFBH Fdnpjo9CucfHV73VvZG4jKw9JIUgrB7OS9Oy11FbZKCIBnsjjqjD3N0gT1nofX44tpGS UydQZmYeEYm0j9OYY5Gt0yTeYUyuX0LrwJQSS2UUmOnFMe3NZFIh9zXCazksbb3f55aR fPwZ6dZwsa3aHK+fv3VTyQX7ik2HYi8SVg4sQnDuZX8kunq/9Z8Lh4N4Yib9Eas53DzK rd+w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=X5VYdc6V; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e17-20020aa79811000000b006438319283esi7854525pfl.73.2023.05.11.07.49.03; Thu, 11 May 2023 07:49:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=X5VYdc6V; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238719AbjEKOno (ORCPT + 99 others); Thu, 11 May 2023 10:43:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35932 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238446AbjEKOma (ORCPT ); Thu, 11 May 2023 10:42:30 -0400 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 69B9CE70; Thu, 11 May 2023 07:38:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1683815931; x=1715351931; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=0jJBkgHs2LiMuos7AIUtcLmWfKMOM9rcJobxYX+NdgU=; b=X5VYdc6V4hZ2obtAIcO+ajjkrgDyuFjj3RkYToGkOc9wq4ogchiivJlK fCuzfBGhdZQf7jadT1z0WSaPcpJUIIlWlwwOIFa2CmcSZIW6hwKJLYP6H SOwmILm2rFXNKs85t3Ib+GK1UCL352Fx/5fXkxCEGbUdmu5dmjF0zp3R/ R0xNBVnkRCcfQhAcwEHu/kTUndExc4E3aONARibUWUXKiXftix+cDKAah ZePkAXCrgCpKRATWliKyr5k5zmCctG+q0K2mC/A4031s2du4kS5HaPopT fA4eLAIHt6tp71bqaQHkecIL/5M/1EGwIR5sI3pGPr4weaimdqpYALulq A==; X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="339812885" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="339812885" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 May 2023 07:38:49 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="730382615" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="730382615" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by orsmga008.jf.intel.com with ESMTP; 11 May 2023 07:38:48 -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 Subject: [PATCH v2 04/11] iommufd: Pass parent hwpt and user_data to iommufd_hw_pagetable_alloc() Date: Thu, 11 May 2023 07:38:37 -0700 Message-Id: <20230511143844.22693-5-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230511143844.22693-1-yi.l.liu@intel.com> References: <20230511143844.22693-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765609628982291168?= X-GMAIL-MSGID: =?utf-8?q?1765609628982291168?= Nested translation has stage-1 and stage-2 page tables. A stage-1 page table is managed by user space, and it needs to work with a stage-2 page table, which is a parent hwpt for the stage-1 hwpt. iommu core already supports accepting parent iommu_domain and user_data to allocate an iommu_domain. This makes iommufd_hw_pagetable_alloc() to accept the parent hwpt and user_data, and relays them to iommu core, to prepare for supporting hw_pagetable allocation with user_data. Also, add a parent pointer in struct iommufd_hw_pagetable for taking and releasing its refcount. Co-developed-by: Nicolin Chen Signed-off-by: Nicolin Chen Signed-off-by: Yi Liu --- drivers/iommu/iommufd/device.c | 2 +- drivers/iommu/iommufd/hw_pagetable.c | 34 ++++++++++++++++++++++--- drivers/iommu/iommufd/iommufd_private.h | 7 ++++- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 4541d785bfd8..58c4deb3cb5d 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -584,7 +584,7 @@ iommufd_device_auto_get_domain(struct iommufd_device *idev, } hwpt = iommufd_hw_pagetable_alloc(idev->ictx, ioas, idev, - immediate_attach); + NULL, NULL, immediate_attach); if (IS_ERR(hwpt)) { destroy_hwpt = ERR_CAST(hwpt); goto out_unlock; diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index b6323ad9c32d..ec9b140939ce 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -24,6 +24,8 @@ void iommufd_hw_pagetable_destroy(struct iommufd_object *obj) if (hwpt->domain) iommu_domain_free(hwpt->domain); + if (hwpt->parent) + refcount_dec(&hwpt->parent->obj.users); refcount_dec(&hwpt->ioas->obj.users); } @@ -61,6 +63,8 @@ int iommufd_hw_pagetable_enforce_cc(struct iommufd_hw_pagetable *hwpt) * @ictx: iommufd context * @ioas: IOAS to associate the domain with * @idev: Device to get an iommu_domain for + * @parent: Optional parent HWPT to associate with the domain with + * @user_data: Optional user_data pointer * @immediate_attach: True if idev should be attached to the hwpt * * Allocate a new iommu_domain and return it as a hw_pagetable. The HWPT @@ -73,14 +77,22 @@ int iommufd_hw_pagetable_enforce_cc(struct iommufd_hw_pagetable *hwpt) */ struct iommufd_hw_pagetable * iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, - struct iommufd_device *idev, bool immediate_attach) + struct iommufd_device *idev, + struct iommufd_hw_pagetable *parent, + union iommu_domain_user_data *user_data, + bool immediate_attach) { const struct iommu_ops *ops = dev_iommu_ops(idev->dev); + struct iommu_domain *parent_domain = NULL; struct iommufd_hw_pagetable *hwpt; + bool type_unmanaged, type_nested; int rc; lockdep_assert_held(&ioas->mutex); + if ((user_data || parent) && !ops->domain_alloc_user) + return ERR_PTR(-EOPNOTSUPP); + hwpt = iommufd_object_alloc(ictx, hwpt, IOMMUFD_OBJ_HW_PAGETABLE); if (IS_ERR(hwpt)) return hwpt; @@ -89,9 +101,15 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, /* Pairs with iommufd_hw_pagetable_destroy() */ refcount_inc(&ioas->obj.users); hwpt->ioas = ioas; + if (parent) { + hwpt->parent = parent; + parent_domain = parent->domain; + refcount_inc(&parent->obj.users); + } if (ops->domain_alloc_user) - hwpt->domain = ops->domain_alloc_user(idev->dev, NULL, NULL); + hwpt->domain = ops->domain_alloc_user(idev->dev, + parent_domain, user_data); else hwpt->domain = iommu_domain_alloc(idev->dev->bus); if (!hwpt->domain) { @@ -99,6 +117,15 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, goto out_abort; } + /* It must be either NESTED or UNMANAGED, depending on parent_domain */ + type_nested = hwpt->domain->type == IOMMU_DOMAIN_NESTED; + type_unmanaged = hwpt->domain->type == IOMMU_DOMAIN_UNMANAGED; + if (WARN_ON((parent_domain && !type_nested) || + (!parent_domain && !type_unmanaged))) { + rc = -EINVAL; + goto out_abort; + } + /* * Set the coherency mode before we do iopt_table_add_domain() as some * iommus have a per-PTE bit that controls it and need to decide before @@ -160,7 +187,8 @@ int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd) } mutex_lock(&ioas->mutex); - hwpt = iommufd_hw_pagetable_alloc(ucmd->ictx, ioas, idev, false); + hwpt = iommufd_hw_pagetable_alloc(ucmd->ictx, ioas, idev, + NULL, NULL, false); if (IS_ERR(hwpt)) { rc = PTR_ERR(hwpt); goto out_unlock; diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 69d6bb61d387..9fe807e0aed6 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -8,6 +8,7 @@ #include #include #include +#include struct iommu_domain; struct iommu_group; @@ -243,6 +244,7 @@ int iommufd_vfio_ioas(struct iommufd_ucmd *ucmd); */ struct iommufd_hw_pagetable { struct iommufd_object obj; + struct iommufd_hw_pagetable *parent; struct iommufd_ioas *ioas; struct iommu_domain *domain; bool auto_domain : 1; @@ -254,7 +256,10 @@ struct iommufd_hw_pagetable { struct iommufd_hw_pagetable * iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, - struct iommufd_device *idev, bool immediate_attach); + struct iommufd_device *idev, + struct iommufd_hw_pagetable *parent, + union iommu_domain_user_data *user_data, + bool immediate_attach); int iommufd_hw_pagetable_enforce_cc(struct iommufd_hw_pagetable *hwpt); int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, struct iommufd_device *idev); From patchwork Thu May 11 14:38:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 92673 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp4432570vqo; Thu, 11 May 2023 07:49:19 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4D+/IGZ/6L+vzM8oruuDGo6E3LBFGeFVAZZTbMBw6H7DZL4BwyEfM1JKOvfpYO0kT2Gjan X-Received: by 2002:a05:6a20:244b:b0:100:a9e3:17d4 with SMTP id t11-20020a056a20244b00b00100a9e317d4mr16720690pzc.0.1683816559216; Thu, 11 May 2023 07:49:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683816559; cv=none; d=google.com; s=arc-20160816; b=WN37Ivlz24A7uN/Nd9QPn2AOeFjIvF0OIOx44lwi0PQqZmgFFhI+3awKXmiSVRQ1jl LxWM7KL2RsTpT3tScoYTWbzpB2nCc+eWq8KadhwhvAOPr0XeVLfLcvfUa2SIXTXKAlLY g9heWFnJzED/YIokAYMSVtzXfvA2BWqER9NjAEMZvoYbAz7tEpQlvMfTqT84Qi1akDTE ulJm8OkihXqq55WT+Gkhq3X7mUBpdALIh1/vQ7DHeSjSELK4RMCGWyUqgH1GpuQAfnf8 /NvHF4R1HpJtUrVOHJ1tNTnYKZgFbuQImJHdKN1YEVH2Y+TuGpy3BYUoXGxV5y4OFzmg Uk2A== 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=q8E4cDEhhSVLcv0LSCwH5BbGOQLSh/RH0HVSlYbKW6Y=; b=f1SyIYrz789+yaRjkMWDVVxgSgsJGG/cDRH8+FXxaqDeKdXmCq8YS/zF0mr3bgyUDx mbfwPEtV1azbH6qr4C7Lw+ey5flrb/7EDRczzGsF0XQRQCIepC9jgcYJxlv+iaXnTLlB I2SAatOGZ/cFl+tW6PsURoVSNBPmwwwCjPNxuGjk0PMVoorHF14MzhAw5s4vjosJCTCA tA+mcP2XE3fOOZs+cgA3tu6aVJH949AWF/XvoxsWDlQ5dfKhC05YmUhGRh1Rr2iEsQco Cs2B3+GjFYyVXciYLUHZvT//i2VGwOGHavwRWCJtwHUlYY2qo0NlqTZvf/3hZvR9/rOh 92yA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=CTZEYr00; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k8-20020a637b48000000b0050300a5cf3csi6965950pgn.469.2023.05.11.07.49.05; Thu, 11 May 2023 07:49:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=CTZEYr00; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238204AbjEKOnk (ORCPT + 99 others); Thu, 11 May 2023 10:43:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35936 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238575AbjEKOmb (ORCPT ); Thu, 11 May 2023 10:42:31 -0400 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6C8E2124A4; Thu, 11 May 2023 07:38:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1683815931; x=1715351931; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Ke4Q2tfTPA1F8lkmM3qM3tQXa//6Jv+EtDRXACRgOKk=; b=CTZEYr00d3JJ1FzJh8Zs3tzAA5yUhl3XN6/a2tEjk1gjfISzDCw9LChP gpcjNNo+3GcQ4omZvLZjFxxhmpzpHEXLiF5PCKRS95tNQcPh+iORDeZh2 m80jAMtv1W+n6gkwMpoeT17FJZ9BNIM5nYc+NUihkYsqE6UYNz2b17UMA boZjnTtzsNZz3gV9p0M0jMFBYvXny/I6jUQzrxrxvAOB0xQRDECmeqcDO bmjmb7FmYU1CIdDjkkvAwHru1yoMjd5Y3LFf3o88YWaJH51D1bWyFsg8V yXw1z8XlyAosEpHCmBwzie0iTjiSJUiq2rz1O2Y/AVaBonoPMG2hU2otT g==; X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="339812903" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="339812903" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 May 2023 07:38:49 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="730382619" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="730382619" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by orsmga008.jf.intel.com with ESMTP; 11 May 2023 07:38:49 -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 Subject: [PATCH v2 05/11] iommufd/hw_pagetable: Do not populate user-managed hw_pagetables Date: Thu, 11 May 2023 07:38:38 -0700 Message-Id: <20230511143844.22693-6-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230511143844.22693-1-yi.l.liu@intel.com> References: <20230511143844.22693-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765609632503834335?= X-GMAIL-MSGID: =?utf-8?q?1765609632503834335?= From: Nicolin Chen A user-managed hw_pagetable does not need to get populated, since it is managed by a guest OS. Move the iopt_table_add_domain and list_add_tail calls into a helper, where the hwpt pointer will be redirected to its hwpt->parent if it's available. Signed-off-by: Nicolin Chen Signed-off-by: Yi Liu --- drivers/iommu/iommufd/hw_pagetable.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index ec9b140939ce..73d7d9d07726 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -58,6 +58,24 @@ int iommufd_hw_pagetable_enforce_cc(struct iommufd_hw_pagetable *hwpt) return 0; } +static int iommufd_hw_pagetable_link_ioas(struct iommufd_hw_pagetable *hwpt) +{ + int rc; + + /* + * Only a parent hwpt needs to be linked to the IOAS. And a hwpt->parent + * must be linked to the IOAS already, when it's being allocated. + */ + if (hwpt->parent) + return 0; + + rc = iopt_table_add_domain(&hwpt->ioas->iopt, hwpt->domain); + if (rc) + return rc; + list_add_tail(&hwpt->hwpt_item, &hwpt->ioas->hwpt_list); + return 0; +} + /** * iommufd_hw_pagetable_alloc() - Get an iommu_domain for a device * @ictx: iommufd context @@ -151,10 +169,9 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, goto out_abort; } - rc = iopt_table_add_domain(&hwpt->ioas->iopt, hwpt->domain); + rc = iommufd_hw_pagetable_link_ioas(hwpt); if (rc) goto out_detach; - list_add_tail(&hwpt->hwpt_item, &hwpt->ioas->hwpt_list); return hwpt; out_detach: From patchwork Thu May 11 14:38:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 92677 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp4435497vqo; Thu, 11 May 2023 07:53:42 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4OddEmgNMEZ8EUf2AzFPp1TtPMaKmRHriX1nk7yv1rZ+qP7A2aWyp9mEAYAjDY43aUVSag X-Received: by 2002:a17:90a:3b0b:b0:23c:ffbf:859e with SMTP id d11-20020a17090a3b0b00b0023cffbf859emr21007298pjc.35.1683816822398; Thu, 11 May 2023 07:53:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683816822; cv=none; d=google.com; s=arc-20160816; b=TJeZShvlbJFGh+LYr0brmZn4wNgzwDHGZ3VW+q0Z5fBOcKGGnLAEE75LTSInEuLKCF D8KFGGlGHivxouCT44YL2i5NAeoyGUgetc63q6Z+b1JTf/PVwouocDrsi7TR2J0p9+Aj nz/voRfYSXOa/ip8+d/MSIbdaov9hIlIgzM0zWn3jyi/fjGzuOGdWknKMduEifAr53hG L7BxlGq2MmubPrv6dfZtdmOE+mZFrpc7JEDZouH9GKMLKykUWYCEekk8MNwnkxGUS0IJ /nQpaNw5oTHc3S1LPOB9S67CS8+3TAtUUw+lNHi4TrSRiecdhK8LtpGhUGKREO+Zc43S x7gQ== 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=6ZB4uVUNPC3JgDbzP+Ku/mMiNo+PjbIwSORoDeLum4w=; b=vW+hAqKgsZs6ZLwSrDBlVfBnVLPjwZ1Uao15TkOLgAgOV7nH9orfkPDEOpmlUNpSrM 87Q+mTYYrriAqUcgE5uHgCd9kURgAxJQSQQ4i6aihjRq60CeMWoUK2dsbH1llm2YxgBW jYRRsh2KNBQpKagJtZZctfqUr4L04GQV7RmCfC6slhRkm4xzXy/DWt5pR3UAuW7xWYm6 kcTPPolh+P3Mrn1pLjpQYqTViHKp8aA76boYkb4zTiOFxRzuh1gvjJvKsTvRW9+rWwxy 09w28L3StMMdbw1Z9ONsQnOkNQ0THxhAcT71YL2ptdAAg7hvHns+vaw8Nqh04jYK1jKj DXyA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=RFVYxI43; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l4-20020a17090a850400b002475eb6480asi13812066pjn.186.2023.05.11.07.53.26; Thu, 11 May 2023 07:53:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=RFVYxI43; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238122AbjEKOnL (ORCPT + 99 others); Thu, 11 May 2023 10:43:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35944 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238016AbjEKOmb (ORCPT ); Thu, 11 May 2023 10:42:31 -0400 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BA12110E40; Thu, 11 May 2023 07:38:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1683815932; x=1715351932; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=HbmzlsdSkuEhZruXf7dIWqcPWSG5KyYfb9S8kTNAT1U=; b=RFVYxI43D3nL6XkJ+iX5c2CkBJv1Zp0m9qhi2Ozm0ewiYshbnau0d3MM 8lA/cMoW1UPlkHnNWvOYdm3OFi/AkCpI31KbKFJ6OnlFvNpCzdE5pDJX9 fhrAiZx+fF2YRmslYcYj/tTpC7SKXT+/RyJ+WypV9EDW/NYw+CQHWKSZA G/82/P4Q3DwZDarHggHekJTUXItv8/HrvzYAgGTWurs+pTrt3tMX6kN00 UfxXSEQi4HqvQdWmOAGco8xqOSUJ3xpNs+qupC7hfOBhG834UvlLTOMT5 W9946kjwepOOYyEuCyXYAbLEh8xaSSIHlTseAckXLe4IrBq2XRIKfFn0H A==; X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="339812912" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="339812912" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 May 2023 07:38:50 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="730382622" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="730382622" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by orsmga008.jf.intel.com with ESMTP; 11 May 2023 07:38:49 -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 Subject: [PATCH v2 06/11] iommufd: IOMMU_HWPT_ALLOC allocation with user data Date: Thu, 11 May 2023 07:38:39 -0700 Message-Id: <20230511143844.22693-7-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230511143844.22693-1-yi.l.liu@intel.com> References: <20230511143844.22693-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765609908560443433?= X-GMAIL-MSGID: =?utf-8?q?1765609908560443433?= IOMMU_HWPT_ALLOC already supports iommu_domain allocation for usersapce. But it can only allocate hw_pagetables linked with IOAS. There are needs to support hw_pagetable allocation with parameters specified by user. For example, in nested translation, user needs to allocate hw_pagetable for the stage-1 translation (e.g. a single I/O page table or a set of I/O page tables) with user data. It also needs provide a stage-2 hw_pagetable which is linked to the GPA IOAS. This extends IOMMU_HWPT_ALLOC to accept user specified parameter and hwpt ID in @pt_id field. Such as the user-managed stage-1 hwpt, which requires a parent hwpt to point to stage-2 translation. enum iommu_hwpt_type is defined to differentiate the user parameters use by different usages. For the allocations that don't require user parameter, IOMMU_HWPT_TYPE_DEFAULT is defined for backward compatibility. Other types would be added by future iommu vendor driver extensions. Co-developed-by: Nicolin Chen Signed-off-by: Nicolin Chen Signed-off-by: Yi Liu --- drivers/iommu/iommufd/hw_pagetable.c | 87 +++++++++++++++++++++++++--- drivers/iommu/iommufd/main.c | 2 +- include/uapi/linux/iommufd.h | 32 +++++++++- 3 files changed, 112 insertions(+), 9 deletions(-) diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index 73d7d9d07726..e84270eb6e49 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -184,11 +184,15 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd) { + struct iommufd_hw_pagetable *hwpt, *parent = NULL; + union iommu_domain_user_data *data = NULL; struct iommu_hwpt_alloc *cmd = ucmd->cmd; - struct iommufd_hw_pagetable *hwpt; + struct iommufd_object *pt_obj; + const struct iommu_ops *ops; struct iommufd_device *idev; struct iommufd_ioas *ioas; - int rc; + int klen = 0; + int rc = 0; if (cmd->flags || cmd->__reserved) return -EOPNOTSUPP; @@ -197,15 +201,81 @@ int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd) if (IS_ERR(idev)) return PTR_ERR(idev); - ioas = iommufd_get_ioas(ucmd->ictx, cmd->pt_id); - if (IS_ERR(ioas)) { - rc = PTR_ERR(ioas); + ops = dev_iommu_ops(idev->dev); + + /* + * All drivers support IOMMU_HWPT_TYPE_DEFAULT, so pass it through. + * For any other hwpt_type, check the ops->domain_alloc_user_data_len + * presence and its result. + */ + if (cmd->hwpt_type != IOMMU_HWPT_TYPE_DEFAULT) { + if (!ops->domain_alloc_user_data_len) { + rc = -EOPNOTSUPP; + goto out_put_idev; + } + klen = ops->domain_alloc_user_data_len(cmd->hwpt_type); + if (WARN_ON(klen < 0)) { + rc = -EINVAL; + goto out_put_pt; + } + } + + pt_obj = iommufd_get_object(ucmd->ictx, cmd->pt_id, IOMMUFD_OBJ_ANY); + if (IS_ERR(pt_obj)) { + rc = -EINVAL; goto out_put_idev; } + switch (pt_obj->type) { + case IOMMUFD_OBJ_IOAS: + ioas = container_of(pt_obj, struct iommufd_ioas, obj); + break; + case IOMMUFD_OBJ_HW_PAGETABLE: + /* pt_id points HWPT only when hwpt_type is !IOMMU_HWPT_TYPE_DEFAULT */ + if (cmd->hwpt_type == IOMMU_HWPT_TYPE_DEFAULT) { + rc = -EINVAL; + goto out_put_pt; + } + + parent = container_of(pt_obj, struct iommufd_hw_pagetable, obj); + /* + * Cannot allocate user-managed hwpt linking to auto_created + * hwpt. If the parent hwpt is already a user-managed hwpt, + * don't allocate another user-managed hwpt linking to it. + */ + if (parent->auto_domain || parent->parent) { + rc = -EINVAL; + goto out_put_pt; + } + ioas = parent->ioas; + break; + default: + rc = -EINVAL; + goto out_put_pt; + } + + if (klen) { + if (!cmd->data_len) { + rc = -EINVAL; + goto out_put_pt; + } + + data = kzalloc(sizeof(*data), GFP_KERNEL); + if (!data) { + rc = -ENOMEM; + goto out_put_pt; + } + + rc = copy_struct_from_user(data, sizeof(*data), + u64_to_user_ptr(cmd->data_uptr), + cmd->data_len); + if (rc) + goto out_free_data; + } + mutex_lock(&ioas->mutex); hwpt = iommufd_hw_pagetable_alloc(ucmd->ictx, ioas, idev, - NULL, NULL, false); + parent, data, false); if (IS_ERR(hwpt)) { rc = PTR_ERR(hwpt); goto out_unlock; @@ -222,7 +292,10 @@ int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd) iommufd_object_abort_and_destroy(ucmd->ictx, &hwpt->obj); out_unlock: mutex_unlock(&ioas->mutex); - iommufd_put_object(&ioas->obj); +out_free_data: + kfree(data); +out_put_pt: + iommufd_put_object(pt_obj); out_put_idev: iommufd_put_object(&idev->obj); return rc; diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index 5c24e8971f09..ac81403ba78e 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -302,7 +302,7 @@ struct iommufd_ioctl_op { static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = { IOCTL_OP(IOMMU_DESTROY, iommufd_destroy, struct iommu_destroy, id), IOCTL_OP(IOMMU_HWPT_ALLOC, iommufd_hwpt_alloc, struct iommu_hwpt_alloc, - __reserved), + data_uptr), IOCTL_OP(IOMMU_DEVICE_GET_HW_INFO, iommufd_device_get_hw_info, struct iommu_hw_info, __reserved), IOCTL_OP(IOMMU_IOAS_ALLOC, iommufd_ioas_alloc_ioctl, diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index e9d42838dcbd..699e735828db 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -347,6 +347,14 @@ struct iommu_vfio_ioas { }; #define IOMMU_VFIO_IOAS _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VFIO_IOAS) +/** + * enum iommu_hwpt_type - IOMMU HWPT Type + * @IOMMU_HWPT_TYPE_DEFAULT: default + */ +enum iommu_hwpt_type { + IOMMU_HWPT_TYPE_DEFAULT, +}; + /** * struct iommu_hwpt_alloc - ioctl(IOMMU_HWPT_ALLOC) * @size: sizeof(struct iommu_hwpt_alloc) @@ -355,12 +363,31 @@ struct iommu_vfio_ioas { * @pt_id: The IOAS to connect this HWPT to * @out_hwpt_id: The ID of the new HWPT * @__reserved: Must be 0 + * @hwpt_type: One of enum iommu_hwpt_type + * @data_len: Length of the type specific data + * @data_uptr: User pointer to the type specific data * * Explicitly allocate a hardware page table object. This is the same object * type that is returned by iommufd_device_attach() and represents the * underlying iommu driver's iommu_domain kernel object. * - * A HWPT will be created with the IOVA mappings from the given IOAS. + * A kernel-managed HWPT will be created with the mappings from the given IOAS. + * The @hwpt_type for its allocation can be set to IOMMU_HWPT_TYPE_DEFAULT, or + * another type (being listed below) to specialize a kernel-managed HWPT. + * + * A user-managed HWPT will be created from a given parent HWPT via @pt_id, in + * which the parent HWPT must be allocated previously via the same ioctl from a + * given IOAS. The @hwpt_type must not be set to IOMMU_HWPT_TYPE_DEFAULT but a + * pre-defined type corresponding to the underlying IOMMU hardware. + * + * If the @hwpt_type is set to IOMMU_HWPT_TYPE_DEFAULT, both the @data_len and + * the @data_uptr will be ignored. Otherwise, both of them must be given. + * + * +==============================+=====================================+===========+ + * | @hwpt_type | Data structure in @data_uptr | @pt_id | + * +------------------------------+-------------------------------------+-----------+ + * | IOMMU_HWPT_TYPE_DEFAULT | N/A | IOAS | + * +------------------------------+-------------------------------------+-----------+ */ struct iommu_hwpt_alloc { __u32 size; @@ -369,6 +396,9 @@ struct iommu_hwpt_alloc { __u32 pt_id; __u32 out_hwpt_id; __u32 __reserved; + __u32 hwpt_type; + __u32 data_len; + __aligned_u64 data_uptr; }; #define IOMMU_HWPT_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HWPT_ALLOC) From patchwork Thu May 11 14:38:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 92666 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp4431451vqo; Thu, 11 May 2023 07:47:36 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4par5ME5J5D9w8EZoDpnseuHxh9n+au/nxVKh9nTSMjIVbNmYfGYQW7WpMyIn1NIKpmV88 X-Received: by 2002:a17:902:db01:b0:1a6:9f09:866d with SMTP id m1-20020a170902db0100b001a69f09866dmr27747573plx.61.1683816455965; Thu, 11 May 2023 07:47:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683816455; cv=none; d=google.com; s=arc-20160816; b=Cj9TsQKwph7PBtZsns2+XZEDiInViRy+V490heQBVy9/JlM0PBWJNBP+ZpJlpA0JSk GZ914Y2BE3/ZTQMIDpdP3hn3o+56gSZjwcDOJ7PZ6x3+IhpW/EM9JuDvDRULZEqYV83Z XKVYQDkodRrPXp7+mBfD+/BAlp4hxQCQpWrwfIL5AWx0Djx6usxrmxVwlNXXh+DF9E9D Bf3wPKG3Ltv+6wvP2bWzYbS0m/DzzBalyNVsAmBKMY1oyeDO+X5QvJt1a18DADESkOfq 1KK4/ztKGd8DZbh+jqx61yV2oURAZC3WnU0uUvvMuome+P05SHfSdpLur7sc8D1M4hCQ yT9A== 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=LkaC6FzvCmtU395pEuk2KwS9HhBRgbsTj40qQhO12So=; b=UHrmG9Id6AzHLmgKvN9w75lF4e2o5EaCP6PwKt7HaZunC7Wjk2OeSpViGRQinT02Uj CER/wTTX47Wa6p0I1pULotf01h8NLuf+Kg0RciFfgP9OCgS/11SKj6T6fsiXes6WQKqT zYOB3Zh27TpT//laTnMd8pyd6Sbci/hVWoh2JH3rU/E9wVCHt/JUNLQjO4MpWSmgBNPK /AJDjTXEtB0Ej9ZcTYmGwCmg95looCQEEi31gzQykfb9uhebpIBHz2TL77deRCyBE7Ma +IgtShrywI6no9It9qzPo/VOBYRqLNNPscv3qV1X24kIQUmkGHx1IIMtKRAnO4UtyF/R 9LFg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=E6MDvM3c; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l8-20020a170903244800b0019bf9b4b5f1si7285974pls.629.2023.05.11.07.47.23; Thu, 11 May 2023 07:47:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=E6MDvM3c; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238698AbjEKOnX (ORCPT + 99 others); Thu, 11 May 2023 10:43:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35960 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238084AbjEKOmd (ORCPT ); Thu, 11 May 2023 10:42:33 -0400 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 636153AA7; Thu, 11 May 2023 07:38:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1683815933; x=1715351933; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=P3Z9fChHliVhZ3K/FQvxa8gf6rEJaR17QbcaA/Ou5nQ=; b=E6MDvM3c53xDpLFWtJ9sWgLMo2jn+QH8HfD1k9iyv6QVIaMpe9n+SBrp nSJvlxiX0GD4GOqX7b/nfiszjjeaWc1qtbudMN9GJzW3H8/85nJjXmXlQ fLvf06QLb5+kXgiYWkYMwK8sueywv85v0esFfs2Xv1EXo7GmpQLwgqorY BLxlu5ltq0DP+3wQIflpmEnmG28tl4lDqEjRMX1U6Xja2s+6WqQrs1sSx uT3aDVvm9hmDkh34o+SUaAxsMSLQmVIl9NHneH4IiUW0k/ArNSgSFbR/D r8miESpVpKFaE2jQ6l6PIdmgh5WuUWoAWuJowxYSvWJCopSDj+XJ+4N8o A==; X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="339812925" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="339812925" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 May 2023 07:38:50 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="730382626" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="730382626" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by orsmga008.jf.intel.com with ESMTP; 11 May 2023 07:38:50 -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 Subject: [PATCH v2 07/11] iommufd: Add IOMMU_HWPT_INVALIDATE Date: Thu, 11 May 2023 07:38:40 -0700 Message-Id: <20230511143844.22693-8-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230511143844.22693-1-yi.l.liu@intel.com> References: <20230511143844.22693-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765609524292119158?= X-GMAIL-MSGID: =?utf-8?q?1765609524292119158?= In nested translation, the stage-1 page table is user-managed and used by IOMMU hardware, so update of any present page table entry in the stage-1 page table should be followed with an IOTLB invalidation. This adds IOMMU_HWPT_INVALIDATE for IOTLB invalidation. Co-developed-by: Nicolin Chen Signed-off-by: Nicolin Chen Signed-off-by: Yi Liu --- drivers/iommu/iommufd/hw_pagetable.c | 45 +++++++++++++++++++++++++ drivers/iommu/iommufd/iommufd_private.h | 9 +++++ drivers/iommu/iommufd/main.c | 3 ++ include/uapi/linux/iommufd.h | 26 ++++++++++++++ 4 files changed, 83 insertions(+) diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index e84270eb6e49..8206367b8d83 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -300,3 +300,48 @@ int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd) iommufd_put_object(&idev->obj); return rc; } + +int iommufd_hwpt_invalidate(struct iommufd_ucmd *ucmd) +{ + struct iommu_hwpt_invalidate *cmd = ucmd->cmd; + struct iommufd_hw_pagetable *hwpt; + u32 user_data_len, klen; + u64 user_ptr; + int rc = 0; + + if (!cmd->data_len || cmd->__reserved) + return -EOPNOTSUPP; + + hwpt = iommufd_get_hwpt(ucmd, cmd->hwpt_id); + if (IS_ERR(hwpt)) + return PTR_ERR(hwpt); + + /* Do not allow any kernel-managed hw_pagetable */ + if (!hwpt->parent) { + rc = -EINVAL; + goto out_put_hwpt; + } + + klen = hwpt->domain->ops->cache_invalidate_user_data_len; + if (!hwpt->domain->ops->cache_invalidate_user || !klen) { + rc = -EOPNOTSUPP; + goto out_put_hwpt; + } + + /* + * Copy the needed fields before reusing the ucmd buffer, this + * avoids memory allocation in this path. + */ + user_ptr = cmd->data_uptr; + user_data_len = cmd->data_len; + + rc = copy_struct_from_user(cmd, klen, + u64_to_user_ptr(user_ptr), user_data_len); + if (rc) + goto out_put_hwpt; + + rc = hwpt->domain->ops->cache_invalidate_user(hwpt->domain, cmd); +out_put_hwpt: + iommufd_put_object(&hwpt->obj); + return rc; +} diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 9fe807e0aed6..1f9f48e45e05 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -268,6 +268,7 @@ iommufd_hw_pagetable_detach(struct iommufd_device *idev); void iommufd_hw_pagetable_destroy(struct iommufd_object *obj); void iommufd_hw_pagetable_abort(struct iommufd_object *obj); int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd); +int iommufd_hwpt_invalidate(struct iommufd_ucmd *ucmd); static inline void iommufd_hw_pagetable_put(struct iommufd_ctx *ictx, struct iommufd_hw_pagetable *hwpt) @@ -279,6 +280,14 @@ static inline void iommufd_hw_pagetable_put(struct iommufd_ctx *ictx, refcount_dec(&hwpt->obj.users); } +static inline struct iommufd_hw_pagetable * +iommufd_get_hwpt(struct iommufd_ucmd *ucmd, u32 id) +{ + return container_of(iommufd_get_object(ucmd->ictx, id, + IOMMUFD_OBJ_HW_PAGETABLE), + struct iommufd_hw_pagetable, obj); +} + struct iommufd_group { struct kref ref; struct mutex lock; diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index ac81403ba78e..d80c312f4ebe 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -270,6 +270,7 @@ union ucmd_buffer { struct iommu_destroy destroy; struct iommu_hwpt_alloc hwpt; struct iommu_hw_info info; + struct iommu_hwpt_invalidate cache; struct iommu_ioas_alloc alloc; struct iommu_ioas_allow_iovas allow_iovas; struct iommu_ioas_copy ioas_copy; @@ -305,6 +306,8 @@ static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = { data_uptr), IOCTL_OP(IOMMU_DEVICE_GET_HW_INFO, iommufd_device_get_hw_info, struct iommu_hw_info, __reserved), + IOCTL_OP(IOMMU_HWPT_INVALIDATE, iommufd_hwpt_invalidate, + struct iommu_hwpt_invalidate, data_uptr), IOCTL_OP(IOMMU_IOAS_ALLOC, iommufd_ioas_alloc_ioctl, struct iommu_ioas_alloc, out_ioas_id), IOCTL_OP(IOMMU_IOAS_ALLOW_IOVAS, iommufd_ioas_allow_iovas, diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index 699e735828db..6b82ef6d268b 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -47,6 +47,7 @@ enum { IOMMUFD_CMD_VFIO_IOAS, IOMMUFD_CMD_HWPT_ALLOC, IOMMUFD_CMD_DEVICE_GET_HW_INFO, + IOMMUFD_CMD_HWPT_INVALIDATE, }; /** @@ -444,4 +445,29 @@ struct iommu_hw_info { __u32 __reserved; }; #define IOMMU_DEVICE_GET_HW_INFO _IO(IOMMUFD_TYPE, IOMMUFD_CMD_DEVICE_GET_HW_INFO) + +/** + * struct iommu_hwpt_invalidate - ioctl(IOMMU_HWPT_INVALIDATE) + * @size: sizeof(struct iommu_hwpt_invalidate) + * @hwpt_id: HWPT ID of target hardware page table for the invalidation + * @data_len: Length of the type specific data + * @__reserved: Must be 0 + * @data_uptr: User pointer to the type specific data + * + * Invalidate the iommu cache for user-managed page table. Modifications + * on user-managed page table should be followed with this operation to + * sync the IOTLB. The data in @data_uptr differs per the hwpt type. + * + * +==============================+========================================+ + * | @hwpt_type | Data structure in @data_uptr | + * +------------------------------+----------------------------------------+ + */ +struct iommu_hwpt_invalidate { + __u32 size; + __u32 hwpt_id; + __u32 data_len; + __u32 __reserved; + __aligned_u64 data_uptr; +}; +#define IOMMU_HWPT_INVALIDATE _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HWPT_INVALIDATE) #endif From patchwork Thu May 11 14:38:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 92667 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp4431569vqo; Thu, 11 May 2023 07:47:46 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6o18zS/+fKyEhKd9gKTrvhCxWwdEpKf6hJMaucHl1fOBKooNCnaukCaDXVnfsV9BxJ/Ws8 X-Received: by 2002:a17:902:e5c4:b0:1a5:329d:b975 with SMTP id u4-20020a170902e5c400b001a5329db975mr24797838plf.66.1683816466543; Thu, 11 May 2023 07:47:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683816466; cv=none; d=google.com; s=arc-20160816; b=YZcZOufFlaLjnvBBNaj36p4EvYMKTlLMmEOmitIk9t3ES0AwdV4kFzM0kzDLSwrEcx gTsh92gGaWZ44QmaWWPiRGYKWjwSIBWAT/eJsXmDVa3FkFyc8khmXX36Rc81JaiEEa7A 1hlzVNK7RoFabeIONrVpy09AyGj/pFMMEzS3mKYnXEdihkV0wD8Y/fn/u5Uro0x90nT4 aM2cuQ5rY8RpEthePLuBxbfYO6gGAESg0rBhH8XHQxqRNNQ6frXHtHuhmVhDsQKtOY0U 3+Nt8e0KCUInLYY3xYcop1OPpeAb9E0T1GDO1rSAEjJmN6U8JPjJwzm0X5MuW7y3nqxZ zbPQ== 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=sPKfSTpCu5+2OsVKuBCAK88w8hMWp/gLxNC4vgEHZ1k=; b=cbk9uHLRmBaFQthxYaHfC1d3foUFCzlixg12eRvgFArtHILJ7k6znel8bCe9Uvr6o+ VDRFoth399PinOi+6W/IFiKWw5Y+gYAM+WTt6Z3VGh15pAbzLahgX5TuMeI0gCEsNMTX 8K9rrvOIhPpzep+RIqa6KTvUu+HM0u7KxUmfpuRnapJDFSSWNG8NHzKfEqLPsjk9MwHA pWV8p+8hZ4ScT4JPK1H0bIERiEk2ckHgKlT0bI5Nh6fLNPoL5WSPcrFrXpxz3z6c+oYg VHSAhej2T0/JatyWr3c/eF7Gk74EMlSOF3W7JEMqm4sqT+Z/MFNPfBDtTCnsgEIT8vTQ 7eqg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="dJ/xm5qW"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k24-20020a170902761800b001a990bcc3f5si6519944pll.392.2023.05.11.07.47.34; Thu, 11 May 2023 07:47:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="dJ/xm5qW"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238713AbjEKOnh (ORCPT + 99 others); Thu, 11 May 2023 10:43:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35962 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238616AbjEKOmd (ORCPT ); Thu, 11 May 2023 10:42:33 -0400 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 79DFC10A1C; Thu, 11 May 2023 07:38:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1683815933; x=1715351933; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ZEAkTT8t+vgMoK2Yq15MLDHZMzyQDBzk03KBMhHFS44=; b=dJ/xm5qWY2/uCAKdlsZBXM0ELQuPdrOavU+lYJwyCiBT1YF1ifu+1QZT ZQ1/5hxWVYHN3h2naIxi/t97AhQAWK1opLyT4YD4rysXIuJ0UvYEMYhVb 51c4gMdJNNIvYCBannX2WigfjpSDSB0soJazXXYlvdU/K5m6X9sDgYBm2 ft3i82XBEXPF+/OOqjnoRIDosVmsw+hOJZQ0VcAC+lXsBpcxn+ZjXj81I Hwd8G1k2Y649Tv0SqpAxyfOsyvJ57AjixGq2iPXrKYxUUCfiwwDC+vNqp PcN0pz+PwlBlSxELPECxMQZI8jOkXPvhACjzViWOnaQPwlii7WqTb8taG A==; X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="339812940" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="339812940" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 May 2023 07:38:51 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="730382631" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="730382631" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by orsmga008.jf.intel.com with ESMTP; 11 May 2023 07:38:51 -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 Subject: [PATCH v2 08/11] iommufd/selftest: Add domain_alloc_user() support in iommu mock Date: Thu, 11 May 2023 07:38:41 -0700 Message-Id: <20230511143844.22693-9-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230511143844.22693-1-yi.l.liu@intel.com> References: <20230511143844.22693-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765609535304781483?= X-GMAIL-MSGID: =?utf-8?q?1765609535304781483?= From: Nicolin Chen Add mock_domain_alloc_user function and iommu_hwpt_selftest data structure to support user space selftest program to allocate domains with user data. Signed-off-by: Nicolin Chen Signed-off-by: Yi Liu --- drivers/iommu/iommufd/hw_pagetable.c | 1 + drivers/iommu/iommufd/iommufd_test.h | 16 ++++++ drivers/iommu/iommufd/selftest.c | 78 +++++++++++++++++++++++++--- include/linux/iommu.h | 3 ++ 4 files changed, 90 insertions(+), 8 deletions(-) diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index 8206367b8d83..47ec7ddd5f5d 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -7,6 +7,7 @@ #include "../iommu-priv.h" #include "iommufd_private.h" +#include "iommufd_test.h" void iommufd_hw_pagetable_destroy(struct iommufd_object *obj) { diff --git a/drivers/iommu/iommufd/iommufd_test.h b/drivers/iommu/iommufd/iommufd_test.h index 3f3644375bf1..e77d57dd8e25 100644 --- a/drivers/iommu/iommufd/iommufd_test.h +++ b/drivers/iommu/iommufd/iommufd_test.h @@ -109,4 +109,20 @@ struct iommu_test_hw_info { __u32 test_reg; }; +/* Should not be equal to any defined value in enum iommu_hwpt_type */ +#define IOMMU_HWPT_TYPE_SELFTTEST 0xdead + +/** + * struct iommu_hwpt_selftest + * + * @flags: page table entry attributes + * @test_config: default iotlb setup (value IOMMU_TEST_IOTLB_DEFAULT) + */ +struct iommu_hwpt_selftest { +#define IOMMU_TEST_FLAG_NESTED (1ULL << 0) + __u64 flags; +#define IOMMU_TEST_IOTLB_DEFAULT 0xbadbeef + __u64 test_config; +}; + #endif diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c index af7459e211ad..616525c5d308 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -84,7 +84,9 @@ void iommufd_test_syz_conv_iova_id(struct iommufd_ucmd *ucmd, struct mock_iommu_domain { struct iommu_domain domain; + struct mock_iommu_domain *parent; struct xarray pfns; + u32 iotlb; }; enum selftest_obj_type { @@ -142,26 +144,69 @@ static void *mock_domain_hw_info(struct device *dev, u32 *length) return info; } -static struct iommu_domain *mock_domain_alloc(unsigned int iommu_domain_type) +static const struct iommu_ops mock_ops; +static struct iommu_domain_ops domain_nested_ops; + +static struct iommu_domain * +__mock_domain_alloc(unsigned int iommu_domain_type, + struct mock_iommu_domain *mock_parent, + const struct iommu_hwpt_selftest *user_cfg) { struct mock_iommu_domain *mock; if (iommu_domain_type == IOMMU_DOMAIN_BLOCKED) return &mock_blocking_domain; - if (WARN_ON(iommu_domain_type != IOMMU_DOMAIN_UNMANAGED)) - return NULL; - mock = kzalloc(sizeof(*mock), GFP_KERNEL); if (!mock) return NULL; - mock->domain.geometry.aperture_start = MOCK_APERTURE_START; - mock->domain.geometry.aperture_end = MOCK_APERTURE_LAST; + mock->parent = mock_parent; + mock->domain.type = iommu_domain_type; mock->domain.pgsize_bitmap = MOCK_IO_PAGE_SIZE; - xa_init(&mock->pfns); + if (mock_parent) { + mock->iotlb = user_cfg->test_config; + mock->domain.ops = &domain_nested_ops; + } else { + mock->domain.geometry.aperture_start = MOCK_APERTURE_START; + mock->domain.geometry.aperture_end = MOCK_APERTURE_LAST; + mock->domain.ops = mock_ops.default_domain_ops; + xa_init(&mock->pfns); + } return &mock->domain; } +static struct iommu_domain *mock_domain_alloc(unsigned int iommu_domain_type) +{ + if (WARN_ON(iommu_domain_type != IOMMU_DOMAIN_BLOCKED && + iommu_domain_type != IOMMU_DOMAIN_UNMANAGED)) + return NULL; + return __mock_domain_alloc(IOMMU_DOMAIN_UNMANAGED, NULL, NULL); +} + +static struct iommu_domain *mock_domain_alloc_user(struct device *dev, + struct iommu_domain *parent, + const union iommu_domain_user_data *user_data) +{ + const struct iommu_hwpt_selftest *user_cfg = (struct iommu_hwpt_selftest *)user_data; + unsigned int iommu_domain_type = IOMMU_DOMAIN_UNMANAGED; + struct mock_iommu_domain *mock_parent = NULL; + + /* Check union iommu_domain_user_data in include/linux/iommu.h */ + static_assert(sizeof(struct iommu_hwpt_selftest) <= 16); + + if (parent) { + if (parent->ops != mock_ops.default_domain_ops) + return NULL; + if (!user_cfg || !(user_cfg->flags & IOMMU_TEST_FLAG_NESTED)) + return NULL; + iommu_domain_type = IOMMU_DOMAIN_NESTED; + mock_parent = container_of(parent, + struct mock_iommu_domain, domain); + } + + return __mock_domain_alloc(iommu_domain_type, mock_parent, user_cfg); +} + static void mock_domain_free(struct iommu_domain *domain) { struct mock_iommu_domain *mock = @@ -290,12 +335,21 @@ static void mock_domain_set_plaform_dma_ops(struct device *dev) */ } +static int mock_domain_user_data_len(u32 hwpt_type) +{ + if (hwpt_type != IOMMU_HWPT_TYPE_SELFTTEST) + return -EOPNOTSUPP; + return sizeof(struct iommu_hwpt_selftest); +}; + static const struct iommu_ops mock_ops = { .owner = THIS_MODULE, .pgsize_bitmap = MOCK_IO_PAGE_SIZE, .hw_info_type = IOMMU_HW_INFO_TYPE_SELFTEST, .hw_info = mock_domain_hw_info, .domain_alloc = mock_domain_alloc, + .domain_alloc_user = mock_domain_alloc_user, + .domain_alloc_user_data_len = mock_domain_user_data_len, .capable = mock_domain_capable, .set_platform_dma_ops = mock_domain_set_plaform_dma_ops, .default_domain_ops = @@ -308,6 +362,11 @@ static const struct iommu_ops mock_ops = { }, }; +static struct iommu_domain_ops domain_nested_ops = { + .free = mock_domain_free, + .attach_dev = mock_domain_nop_attach, +}; + static struct iommu_device mock_iommu_device = { .ops = &mock_ops, }; @@ -324,7 +383,10 @@ get_md_pagetable(struct iommufd_ucmd *ucmd, u32 mockpt_id, if (IS_ERR(obj)) return ERR_CAST(obj); hwpt = container_of(obj, struct iommufd_hw_pagetable, obj); - if (hwpt->domain->ops != mock_ops.default_domain_ops) { + if ((hwpt->domain->type == IOMMU_DOMAIN_UNMANAGED && + hwpt->domain->ops != mock_ops.default_domain_ops) || + (hwpt->domain->type == IOMMU_DOMAIN_NESTED && + hwpt->domain->ops != &domain_nested_ops)) { iommufd_put_object(&hwpt->obj); return ERR_PTR(-EINVAL); } diff --git a/include/linux/iommu.h b/include/linux/iommu.h index c2d0fa3e2e18..dff4fc2b2014 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -231,6 +231,9 @@ struct iommu_iotlb_gather { * include/uapi/linux/iommufd.h. */ union iommu_domain_user_data { +#ifdef CONFIG_IOMMUFD_TEST + __u64 test[2]; +#endif }; /** From patchwork Thu May 11 14:38:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 92669 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp4432177vqo; Thu, 11 May 2023 07:48:45 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7duNVH0FSURtbjrTuIl/Jkdt5PMUuItQ2yWtuliH28Q+V8L41/g5stnbCEsZzM5KAccI/m X-Received: by 2002:a05:6a20:9153:b0:101:3600:6aa5 with SMTP id x19-20020a056a20915300b0010136006aa5mr12058986pzc.3.1683816525398; Thu, 11 May 2023 07:48:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683816525; cv=none; d=google.com; s=arc-20160816; b=gKoF9Aa2o2221Q3mXwT3Hs0C81KHUZuoauM32gr4NdRDhJbaA/IdT8ayHmK8YooedU TdSQYUY9nOpoIHtf2cfn04nT859GY/VNGhw9Y0ia5eS7IfXiSLXSwvQXDkkkETaOwtyW x87hFL9tklWLshmhza/oARJb2s7IgHYwrrDRFDQK3IRvHUiYcqydOqNCuHHgnVs2tc/A 6uw2enL5iCDuHtlNfODJWW7j8rSjULZkAitnf8wmZwA+zWYeOzApNG15Go8d165Togk4 qmUiau9dPP6Esb/armAcmCL889lLm0kNpFzWxiv9WIhy1kNaDuBFI3OiJnZF1gWJqB8v UdHw== 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=xQnMV99KdgdKl8VG19bEG7+g2Pz9wLM/IywMoaCLT9M=; b=lNutETRuQxVGpJ0sefcU5o1OtpPRHT/eSMQDR8l3urzoWHxI+zgBoIs5TfUEC0YIha iOWJM2Z1risMopBRPqWsMNgIV3z/uFLQLZF2gCFo3DsCd8pH1AijJmyoS+nukATT9iJ3 yNQEh1rmssEU5bGaE5HBTazExgKoS9IEcsykt40WqdbolqJDqDCywBv8kFyib9XzDJIZ eXsNkuI2SQqGcoZMQ16yQTfkM8v/R55XHaSxyJyWNQqrKDNdXdBSja+PSLTgzUyOpfyY nRYILFahVWFJGWpyLB1aabLVj5/IB3ZKIHzdncR6BXNIxHTUEgJnmWOQTCgWqHWrp5Hw r5+g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=CTTqhLot; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z195-20020a6333cc000000b005304f692247si5477182pgz.458.2023.05.11.07.48.17; Thu, 11 May 2023 07:48:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=CTTqhLot; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238471AbjEKOna (ORCPT + 99 others); Thu, 11 May 2023 10:43:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35976 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238179AbjEKOme (ORCPT ); Thu, 11 May 2023 10:42:34 -0400 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8A3D8124B3; Thu, 11 May 2023 07:38: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=1683815934; x=1715351934; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=BS+fRsn96RzSNGa/WgYbuBJ4b2bz4r4Rvr0liZj3b6M=; b=CTTqhLot24hB+lv+SQRz1Be8uTEPB17XSF3FsuOhesCO438EAXaxQ+/T I8i7UqOKerCSA4m8GjT5WJzA2T4/ume+AmtJSfMNdeSiSPcRx2oeeQ3Lt L3WGkjSRI/zbtgxSCu8VAagzZBlXtjaSW4/gsSPOMJj7y/3l7uVQGMUkH pWR9DGTAazuUUBbbzh/Po3WFv6X7ygnIQv7h3jUaLu7bSXJfXjlDZFLar g/9fa2qKzvPUqaGstgLiwk8/thE+Bei0g6m9eJdLg57jkb6MFAoBdOB2o 9DgZS7r6UTDnUta5Dc7lSRTIVRlRP6AY/akROG7MtV5aio2huBdh/Ph1+ g==; X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="339812954" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="339812954" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 May 2023 07:38:52 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="730382634" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="730382634" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by orsmga008.jf.intel.com with ESMTP; 11 May 2023 07:38:52 -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 Subject: [PATCH v2 09/11] iommufd/selftest: Add coverage for IOMMU_HWPT_ALLOC with user data Date: Thu, 11 May 2023 07:38:42 -0700 Message-Id: <20230511143844.22693-10-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230511143844.22693-1-yi.l.liu@intel.com> References: <20230511143844.22693-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765609596878121261?= X-GMAIL-MSGID: =?utf-8?q?1765609596878121261?= From: Nicolin Chen The IOMMU_HWPT_ALLOC ioctl now supports passing user_data to allocate a customized domain. Add its coverage for both a regular domain case and a nested domain case. Signed-off-by: Nicolin Chen Signed-off-by: Yi Liu --- tools/testing/selftests/iommu/iommufd.c | 114 +++++++++++++++++- tools/testing/selftests/iommu/iommufd_utils.h | 36 ++++++ 2 files changed, 149 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c index fa2324741ad2..568f80f596ad 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -122,6 +122,7 @@ TEST_F(iommufd, cmd_length) TEST_LENGTH(iommu_option, IOMMU_OPTION); TEST_LENGTH(iommu_vfio_ioas, IOMMU_VFIO_IOAS); TEST_LENGTH(iommu_hw_info, IOMMU_DEVICE_GET_HW_INFO); + TEST_LENGTH(iommu_hwpt_alloc, IOMMU_HWPT_ALLOC); #undef TEST_LENGTH } @@ -194,6 +195,7 @@ FIXTURE_VARIANT(iommufd_ioas) { unsigned int mock_domains; unsigned int memory_limit; + bool new_hwpt; }; FIXTURE_SETUP(iommufd_ioas) @@ -233,6 +235,12 @@ FIXTURE_VARIANT_ADD(iommufd_ioas, mock_domain) .mock_domains = 1, }; +FIXTURE_VARIANT_ADD(iommufd_ioas, mock_domain_hwpt) +{ + .mock_domains = 1, + .new_hwpt = true, +}; + FIXTURE_VARIANT_ADD(iommufd_ioas, two_mock_domain) { .mock_domains = 2, @@ -260,6 +268,93 @@ TEST_F(iommufd_ioas, ioas_destroy) } } +TEST_F(iommufd_ioas, hwpt_alloc) +{ + uint32_t new_hwpt_id = 0; + + if (self->stdev_id && self->device_id) { + test_cmd_hwpt_alloc(self->device_id, self->ioas_id, &new_hwpt_id); + test_cmd_mock_domain_replace(self->stdev_id, new_hwpt_id); + /* hw_pagetable cannot be freed if a device is attached to it */ + EXPECT_ERRNO(EBUSY, _test_ioctl_destroy(self->fd, new_hwpt_id)); + + /* Detach from the new hw_pagetable and try again */ + test_cmd_mock_domain_replace(self->stdev_id, self->ioas_id); + test_ioctl_destroy(new_hwpt_id); + } else { + test_err_cmd_hwpt_alloc(ENOENT, self->device_id, + self->ioas_id, &new_hwpt_id); + test_err_mock_domain_replace(ENOENT, + self->stdev_id, new_hwpt_id); + } +} + +TEST_F(iommufd_ioas, nested_hwpt_alloc) +{ + uint32_t nested_hwpt_id[2] = {}; + uint32_t parent_hwpt_id = 0; + uint32_t test_hwpt_id = 0; + + if (self->device_id) { + /* Negative tests */ + test_err_cmd_hwpt_alloc(ENOENT, self->ioas_id, self->device_id, + &test_hwpt_id); + test_err_cmd_hwpt_alloc(EINVAL, self->device_id, + self->device_id, &test_hwpt_id); + + /* Allocate two nested hwpts sharing one common parent hwpt */ + test_cmd_hwpt_alloc(self->device_id, self->ioas_id, + &parent_hwpt_id); + + test_cmd_hwpt_alloc_nested(self->device_id, parent_hwpt_id, + &nested_hwpt_id[0]); + test_cmd_hwpt_alloc_nested(self->device_id, parent_hwpt_id, + &nested_hwpt_id[1]); + + /* Negative test: a nested hwpt on top of a nested hwpt */ + test_err_cmd_hwpt_alloc_nested(EINVAL, self->device_id, + nested_hwpt_id[0], + &test_hwpt_id); + /* Negative test: parent hwpt now cannot be freed */ + EXPECT_ERRNO(EBUSY, + _test_ioctl_destroy(self->fd, parent_hwpt_id)); + + /* Attach device to nested_hwpt_id[0] that then will be busy */ + test_cmd_mock_domain_replace(self->stdev_id, + nested_hwpt_id[0]); + EXPECT_ERRNO(EBUSY, + _test_ioctl_destroy(self->fd, nested_hwpt_id[0])); + + /* Switch from nested_hwpt_id[0] to nested_hwpt_id[1] */ + test_cmd_mock_domain_replace(self->stdev_id, + nested_hwpt_id[1]); + EXPECT_ERRNO(EBUSY, + _test_ioctl_destroy(self->fd, nested_hwpt_id[1])); + test_ioctl_destroy(nested_hwpt_id[0]); + + /* Detach from nested_hwpt_id[1] and destroy it */ + test_cmd_mock_domain_replace(self->stdev_id, parent_hwpt_id); + test_ioctl_destroy(nested_hwpt_id[1]); + + /* Detach from the parent hw_pagetable and destroy it */ + test_cmd_mock_domain_replace(self->stdev_id, self->ioas_id); + test_ioctl_destroy(parent_hwpt_id); + } else { + test_err_cmd_hwpt_alloc(ENOENT, self->device_id, self->ioas_id, + &parent_hwpt_id); + test_err_cmd_hwpt_alloc_nested(ENOENT, self->device_id, + parent_hwpt_id, + &nested_hwpt_id[0]); + test_err_cmd_hwpt_alloc_nested(ENOENT, self->device_id, + parent_hwpt_id, + &nested_hwpt_id[1]); + test_err_mock_domain_replace(ENOENT, self->stdev_id, + nested_hwpt_id[0]); + test_err_mock_domain_replace(ENOENT, self->stdev_id, + nested_hwpt_id[1]); + } +} + TEST_F(iommufd_ioas, hwpt_attach) { /* Create a device attached directly to a hwpt */ @@ -663,6 +758,8 @@ TEST_F(iommufd_ioas, access_pin) MOCK_FLAGS_ACCESS_CREATE_NEEDS_PIN_PAGES); for (npages = 1; npages < BUFFER_SIZE / PAGE_SIZE; npages++) { + uint32_t new_hwpt_id = 0; + uint32_t mock_device_id; uint32_t mock_stdev_id; uint32_t mock_hwpt_id; @@ -696,12 +793,27 @@ TEST_F(iommufd_ioas, access_pin) _IOMMU_TEST_CMD(IOMMU_TEST_OP_ACCESS_PAGES), &access_cmd)); test_cmd_mock_domain(self->ioas_id, &mock_stdev_id, - &mock_hwpt_id, NULL); + &mock_hwpt_id, &mock_device_id); check_map_cmd.id = mock_hwpt_id; + if (variant->new_hwpt) { + test_cmd_hwpt_alloc(mock_device_id, self->ioas_id, + &new_hwpt_id); + test_cmd_mock_domain_replace(mock_stdev_id, + new_hwpt_id); + check_map_cmd.id = new_hwpt_id; + } else { + check_map_cmd.id = mock_hwpt_id; + } ASSERT_EQ(0, ioctl(self->fd, _IOMMU_TEST_CMD(IOMMU_TEST_OP_MD_CHECK_MAP), &check_map_cmd)); + if (variant->new_hwpt) { + /* Detach from the new hwpt for its destroy() */ + test_cmd_mock_domain_replace(mock_stdev_id, + mock_hwpt_id); + test_ioctl_destroy(new_hwpt_id); + } test_ioctl_destroy(mock_stdev_id); test_cmd_destroy_access_pages( access_cmd.id, diff --git a/tools/testing/selftests/iommu/iommufd_utils.h b/tools/testing/selftests/iommu/iommufd_utils.h index 8dced7ef9118..e2c26168ec89 100644 --- a/tools/testing/selftests/iommu/iommufd_utils.h +++ b/tools/testing/selftests/iommu/iommufd_utils.h @@ -118,6 +118,42 @@ static int _test_cmd_hwpt_alloc(int fd, __u32 device_id, __u32 pt_id, #define test_cmd_hwpt_alloc(device_id, pt_id, hwpt_id) \ ASSERT_EQ(0, _test_cmd_hwpt_alloc(self->fd, device_id, pt_id, hwpt_id)) +#define test_err_cmd_hwpt_alloc(_errno, device_id, pt_id, hwpt_id) \ + EXPECT_ERRNO(_errno, _test_cmd_hwpt_alloc(self->fd, device_id, \ + pt_id, hwpt_id)) + +static int _test_cmd_hwpt_alloc_nested(int fd, __u32 device_id, __u32 parent_id, + __u32 *hwpt_id) +{ + struct iommu_hwpt_selftest data = { + .flags = IOMMU_TEST_FLAG_NESTED, + .test_config = IOMMU_TEST_IOTLB_DEFAULT, + }; + struct iommu_hwpt_alloc cmd = { + .size = sizeof(cmd), + .dev_id = device_id, + .pt_id = parent_id, + .hwpt_type = IOMMU_HWPT_TYPE_SELFTTEST, + .data_len = sizeof(data), + .data_uptr = (uint64_t)&data, + }; + int ret; + + ret = ioctl(fd, IOMMU_HWPT_ALLOC, &cmd); + if (ret) + return ret; + if (hwpt_id) + *hwpt_id = cmd.out_hwpt_id; + return 0; +} + +#define test_cmd_hwpt_alloc_nested(device_id, parent_id, hwpt_id) \ + ASSERT_EQ(0, _test_cmd_hwpt_alloc_nested(self->fd, device_id, \ + parent_id, hwpt_id)) +#define test_err_cmd_hwpt_alloc_nested(_errno, device_id, parent_id, hwpt_id) \ + EXPECT_ERRNO(_errno, \ + _test_cmd_hwpt_alloc_nested(self->fd, device_id, \ + parent_id, hwpt_id)) static int _test_cmd_access_replace_ioas(int fd, __u32 access_id, unsigned int ioas_id) From patchwork Thu May 11 14:38:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 92670 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp4432215vqo; Thu, 11 May 2023 07:48:49 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4UZcuYZivNvBbUULI/ZhM6UCQpHy9zzIhMVMyq0RaGDtnMcowXwUs3ggVs2khTTYLlHebR X-Received: by 2002:a05:6a20:4c8:b0:ec:6039:f76f with SMTP id 8-20020a056a2004c800b000ec6039f76fmr20968224pzd.11.1683816529260; Thu, 11 May 2023 07:48:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683816529; cv=none; d=google.com; s=arc-20160816; b=RZREMa+55YO6CejFfWrto+DH2Qu2X5hG/tvY5nGthm5gZ2bOURVkL7UdChTrjtfAsJ WjXQEHdG1/+vefx/KdEoELPHkErYQWNt3NtPhH05hyxBuefYBmNcP9XJvCRIwp9BUNw7 No+0lkl/NdabtwqOcD8RSTbxiyAWcE3VLztrbPqgqEiUf+xzZp617YckFNzrD/s+BcKS RIj43rf4s0zUx33H2M6E1OLFLARvfqAKlS3FiBlYkmHuEoGB0GU22lRCLW/yMFFCXiBS JjqI/FVEOcStzi2CuvEZduYpjzSvqDbJRVCGOJBNenKoYu5b5qW2QDeOwtkDccFTtoTa shBw== 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=8Y53MHJkxxVKWkSXsSb79qUOnGmlaMGEAC+gdbgEUTE=; b=EDpVOVXLAH7C7zpwGkevX855uakI+SLMgd9J619sEwiUC2IbnsaddxHfDr0pe5PGXl VGHbXt0rFDW6xDhPbTo2RYi7iMZk86vHFV+p8YnADXx+jUlgwD1JvTNIW28DLRTV+xSA Ry/VAI1cjAEtQkNIRB0DAScr5+Ky22v+odRYMlAa9XU/1ciRRV2/wyYzQK23Fx1SN7S3 9ezGqYtL2qM3/RDdMb1hIgFQf/58aRulq5mQGADJzVNJR9vG/V+aOmomjdQbM7YNO+Ux 5j77jfGsIB5PvPRyV0eWm3wQufeV6NoZSPN3zE3mWyCugEF7wKG3q1PS0c/WgZjXVbAp lVvA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Mz1pdwkL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t17-20020a639551000000b0050c0a7a2742si6857604pgn.350.2023.05.11.07.48.35; Thu, 11 May 2023 07:48:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Mz1pdwkL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238533AbjEKOnd (ORCPT + 99 others); Thu, 11 May 2023 10:43:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35978 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237900AbjEKOme (ORCPT ); Thu, 11 May 2023 10:42:34 -0400 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 46F3D10E52; Thu, 11 May 2023 07:38:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1683815935; x=1715351935; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=dFwMxZt/0jrWmtMcQrcBRo0nxvp+usQpsnuOT7m4UGU=; b=Mz1pdwkLaywwyl6CUDeg+DKal+5Tjv/D13JxOyIuv2FAmxxcmgOe205Z 2e11+v+TaZh5yTLquROqIRJ68Bm22osDKLEb+9b9SlZCVDWJzccD5v6IM MCPxt5KgWoyWX6bnXFqGVyolGfmXf++6eoCUrxE1JCl3w17FXmrIARAHe uOX+nw40/mXmeQQqf9OZOKZggMx735YfGQts+AO/xF5AR2x5qe+6V5igs i0Xr5fB/jfNojKOizJxr1rh1ok3upZ3TMgcb5tTgbImdlZorIAQDIxKtM pUs4HlABBVLHb36zq54TRK48fyHJa95xRYxF4bmsftynqom9LwjBc7c6a A==; X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="339812970" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="339812970" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 May 2023 07:38:53 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="730382638" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="730382638" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by orsmga008.jf.intel.com with ESMTP; 11 May 2023 07:38:52 -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 Subject: [PATCH v2 10/11] iommufd/selftest: Add IOMMU_TEST_OP_MD_CHECK_IOTLB test op Date: Thu, 11 May 2023 07:38:43 -0700 Message-Id: <20230511143844.22693-11-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230511143844.22693-1-yi.l.liu@intel.com> References: <20230511143844.22693-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765609600908695336?= X-GMAIL-MSGID: =?utf-8?q?1765609600908695336?= From: Nicolin Chen This allows to test whether IOTLB has been invalidated or not. Signed-off-by: Nicolin Chen Signed-off-by: Yi Liu --- drivers/iommu/iommufd/iommufd_test.h | 4 ++++ drivers/iommu/iommufd/selftest.c | 22 +++++++++++++++++++ tools/testing/selftests/iommu/iommufd.c | 4 ++++ tools/testing/selftests/iommu/iommufd_utils.h | 14 ++++++++++++ 4 files changed, 44 insertions(+) diff --git a/drivers/iommu/iommufd/iommufd_test.h b/drivers/iommu/iommufd/iommufd_test.h index e77d57dd8e25..ec904931df69 100644 --- a/drivers/iommu/iommufd/iommufd_test.h +++ b/drivers/iommu/iommufd/iommufd_test.h @@ -19,6 +19,7 @@ enum { IOMMU_TEST_OP_SET_TEMP_MEMORY_LIMIT, IOMMU_TEST_OP_MOCK_DOMAIN_REPLACE, IOMMU_TEST_OP_ACCESS_REPLACE_IOAS, + IOMMU_TEST_OP_MD_CHECK_IOTLB, }; enum { @@ -95,6 +96,9 @@ struct iommu_test_cmd { struct { __u32 ioas_id; } access_replace_ioas; + struct { + __u32 iotlb; + } check_iotlb; }; __u32 last; }; diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c index 616525c5d308..57530e7d9524 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -678,6 +678,25 @@ static int iommufd_test_md_check_refs(struct iommufd_ucmd *ucmd, return 0; } +static int iommufd_test_md_check_iotlb(struct iommufd_ucmd *ucmd, + u32 mockpt_id, u32 iotlb) +{ + struct iommufd_hw_pagetable *hwpt; + struct mock_iommu_domain *mock; + int rc = 0; + + hwpt = get_md_pagetable(ucmd, mockpt_id, &mock); + if (IS_ERR(hwpt)) + return PTR_ERR(hwpt); + + mock = container_of(hwpt->domain, struct mock_iommu_domain, domain); + + if (iotlb != mock->iotlb) + rc = -EINVAL; + iommufd_put_object(&hwpt->obj); + return rc; +} + struct selftest_access { struct iommufd_access *access; struct file *file; @@ -1091,6 +1110,9 @@ int iommufd_test(struct iommufd_ucmd *ucmd) return iommufd_test_md_check_refs( ucmd, u64_to_user_ptr(cmd->check_refs.uptr), cmd->check_refs.length, cmd->check_refs.refs); + case IOMMU_TEST_OP_MD_CHECK_IOTLB: + return iommufd_test_md_check_iotlb(ucmd, cmd->id, + cmd->check_iotlb.iotlb); case IOMMU_TEST_OP_CREATE_ACCESS: return iommufd_test_create_access(ucmd, cmd->id, cmd->create_access.flags); diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c index 568f80f596ad..afbe23cecb54 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -310,6 +310,10 @@ TEST_F(iommufd_ioas, nested_hwpt_alloc) &nested_hwpt_id[0]); test_cmd_hwpt_alloc_nested(self->device_id, parent_hwpt_id, &nested_hwpt_id[1]); + test_cmd_hwpt_check_iotlb(nested_hwpt_id[0], + IOMMU_TEST_IOTLB_DEFAULT); + test_cmd_hwpt_check_iotlb(nested_hwpt_id[1], + IOMMU_TEST_IOTLB_DEFAULT); /* Negative test: a nested hwpt on top of a nested hwpt */ test_err_cmd_hwpt_alloc_nested(EINVAL, self->device_id, diff --git a/tools/testing/selftests/iommu/iommufd_utils.h b/tools/testing/selftests/iommu/iommufd_utils.h index e2c26168ec89..7d9efe3f7e0d 100644 --- a/tools/testing/selftests/iommu/iommufd_utils.h +++ b/tools/testing/selftests/iommu/iommufd_utils.h @@ -122,6 +122,20 @@ static int _test_cmd_hwpt_alloc(int fd, __u32 device_id, __u32 pt_id, EXPECT_ERRNO(_errno, _test_cmd_hwpt_alloc(self->fd, device_id, \ pt_id, hwpt_id)) +#define test_cmd_hwpt_check_iotlb(hwpt_id, expected) \ + ({ \ + struct iommu_test_cmd test_cmd = { \ + .size = sizeof(test_cmd), \ + .op = IOMMU_TEST_OP_MD_CHECK_IOTLB, \ + .id = hwpt_id, \ + .check_iotlb = { .iotlb = expected }, \ + }; \ + ASSERT_EQ(0, \ + ioctl(self->fd, \ + _IOMMU_TEST_CMD(IOMMU_TEST_OP_MD_CHECK_IOTLB), \ + &test_cmd)); \ + }) + static int _test_cmd_hwpt_alloc_nested(int fd, __u32 device_id, __u32 parent_id, __u32 *hwpt_id) { From patchwork Thu May 11 14:38:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 92668 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp4431740vqo; Thu, 11 May 2023 07:48:04 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7UVjbbXzwIz6aLM6YqMMv9CLOF+IyeUqowK5QShPNL+Rt0AwNctMuxBjyDCj+68F0gCupc X-Received: by 2002:a17:902:c94b:b0:1ab:14f2:e7e6 with SMTP id i11-20020a170902c94b00b001ab14f2e7e6mr26046150pla.65.1683816483736; Thu, 11 May 2023 07:48:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683816483; cv=none; d=google.com; s=arc-20160816; b=JmkEwyVa8qhtuCtm7lG1m4z2NjdtI7yokcMjssbh0zC4gFNwzxzqjA+CibzkU3kIXb uHCBsBCtU4DFF/WlK/4LfMcxRiIA13yGrGOXzxZIIdMRbLiPSaYmgD8OxSj7flDZLCZT pEIog/Nem/cCA9f852/gA9l13jR/axDShxyFzpLojpu4TjhmiqXzi8DKDysN9MMLvxIQ UNLnfOnwyvAKUAvDw8V1miGD8u//baRChG1KEGTQkym0jaiiR30qQHOuFoAsPxcYCSw4 zPGMog0YCtR4gM8XtEwCV0ul7mTno4/yWjj5Qj3syR6rl9JuiwgVLObzbbRHtHxOcdmE J/aQ== 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=iDn0yTaROtyNF/2C3uxKgFPVBYLjVe8AL/QcwPvUbyY=; b=wJ2U/4+LBdTSlW+s/g1hm/kD5isW1zXcQXtm0lRjXJp0x/liitzuWAT7I9JDIyWRkU geUIhbUkZvER97nFnEeJ+yhPQ83aACvcnX4BwfYL9W4hCp2c8Ad1sFDz7d3XmqeWq9LZ 3yW1wHxjybfPJ3Y4+trtvowvHDOORPbh9pe4hx8CNWlQL/BE5NLYrc861g0LSuIsAj6i Gjf9EftT71UJOipwgICwO0RNZoziGUEePBlC9RXonors8HplDohZa29cOAPUAbeBqxIk 4MqSfBZTBBMCWEUO2Eebgu9A2Tnia1xeA/ViAmluBqhlzyYNgkeYGXkArNo7fQxlE0xp /mcQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Rr3708i8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y1-20020a170902864100b001a97a54c3a2si6586089plt.197.2023.05.11.07.47.51; Thu, 11 May 2023 07:48:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Rr3708i8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238516AbjEKOn2 (ORCPT + 99 others); Thu, 11 May 2023 10:43:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35982 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238465AbjEKOmf (ORCPT ); Thu, 11 May 2023 10:42:35 -0400 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C096211569; Thu, 11 May 2023 07:38:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1683815935; x=1715351935; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=AB/V+lOcmgiua42crOM5BnIdwENlAyxry4E4lQjQqZw=; b=Rr3708i8GzoJIrkMtm5IHsfGcqxS2VgOenRPnfdqoz3VpUQYeAmSy0UU mmXgqlu6yYZNTMYiY8UeI2lGZ73Y2OIeu+1yRz/xuRr2u2Uki2hKJSISV ud8sDhUIRTNlv9d3T5dbFGa0P0Rh6Vas7nchFoan+enzcoADgyURgtZ1J dAivKkhJDD/MX9YzeGURvHBAUOiwvzwv+WwuNXe2fFscoGEFBr46Z3cdr 4DtcGhODOMsyCuHIVr22miCzgVhkiZBn6CuSw4XFrRuux0dlFgU39JyqP /T3tkZFDHj8DEZz0ZT04AM/cnRX9xHeNxsf4NF0rb1LEB37Dd3ZgffG3j g==; X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="339812989" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="339812989" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 May 2023 07:38:54 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="730382644" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="730382644" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by orsmga008.jf.intel.com with ESMTP; 11 May 2023 07:38:54 -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 Subject: [PATCH v2 11/11] iommufd/selftest: Add coverage for IOMMU_HWPT_INVALIDATE ioctl Date: Thu, 11 May 2023 07:38:44 -0700 Message-Id: <20230511143844.22693-12-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230511143844.22693-1-yi.l.liu@intel.com> References: <20230511143844.22693-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765609552994039658?= X-GMAIL-MSGID: =?utf-8?q?1765609552994039658?= From: Nicolin Chen Add a mock_domain_cache_invalidate_user() and a corresponding struct iommu_hwpt_invalidate_selftest, to allow to test IOMMU_HWPT_INVALIDATE from user space, by using the new IOMMU_TEST_OP_MD_CHECK_IOTLB. Signed-off-by: Nicolin Chen Signed-off-by: Yi Liu --- drivers/iommu/iommufd/iommufd_test.h | 10 ++++++++++ drivers/iommu/iommufd/selftest.c | 19 ++++++++++++++++++ tools/testing/selftests/iommu/iommufd.c | 8 ++++++++ tools/testing/selftests/iommu/iommufd_utils.h | 20 +++++++++++++++++++ 4 files changed, 57 insertions(+) diff --git a/drivers/iommu/iommufd/iommufd_test.h b/drivers/iommu/iommufd/iommufd_test.h index ec904931df69..c5ca69bd07d1 100644 --- a/drivers/iommu/iommufd/iommufd_test.h +++ b/drivers/iommu/iommufd/iommufd_test.h @@ -129,4 +129,14 @@ struct iommu_hwpt_selftest { __u64 test_config; }; +/** + * struct iommu_hwpt_invalidate_selftest + * + * @flags: invalidate flags + */ +struct iommu_hwpt_invalidate_selftest { +#define IOMMU_TEST_INVALIDATE_ALL (1ULL << 0) + __u64 flags; +}; + #endif diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c index 57530e7d9524..3c0051442b4d 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -362,9 +362,28 @@ static const struct iommu_ops mock_ops = { }, }; +static int mock_domain_cache_invalidate_user(struct iommu_domain *domain, + void *user_data) +{ + struct iommu_hwpt_invalidate_selftest *inv_info = user_data; + struct mock_iommu_domain *mock = + container_of(domain, struct mock_iommu_domain, domain); + + if (domain->type != IOMMU_DOMAIN_NESTED || !mock->parent) + return -EINVAL; + + if (inv_info->flags & IOMMU_TEST_INVALIDATE_ALL) + mock->iotlb = 0; + + return 0; +} + static struct iommu_domain_ops domain_nested_ops = { .free = mock_domain_free, .attach_dev = mock_domain_nop_attach, + .cache_invalidate_user = mock_domain_cache_invalidate_user, + .cache_invalidate_user_data_len = + sizeof(struct iommu_hwpt_invalidate_selftest), }; static struct iommu_device mock_iommu_device = { diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c index afbe23cecb54..031e222cebf9 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -123,6 +123,7 @@ TEST_F(iommufd, cmd_length) TEST_LENGTH(iommu_vfio_ioas, IOMMU_VFIO_IOAS); TEST_LENGTH(iommu_hw_info, IOMMU_DEVICE_GET_HW_INFO); TEST_LENGTH(iommu_hwpt_alloc, IOMMU_HWPT_ALLOC); + TEST_LENGTH(iommu_hwpt_invalidate, IOMMU_HWPT_INVALIDATE); #undef TEST_LENGTH } @@ -323,6 +324,13 @@ TEST_F(iommufd_ioas, nested_hwpt_alloc) EXPECT_ERRNO(EBUSY, _test_ioctl_destroy(self->fd, parent_hwpt_id)); + /* hwpt_invalidate only supports a user-managed hwpt (nested) */ + test_err_cmd_hwpt_invalidate(EINVAL, parent_hwpt_id); + test_cmd_hwpt_invalidate(nested_hwpt_id[0]); + test_cmd_hwpt_check_iotlb(nested_hwpt_id[0], 0); + test_cmd_hwpt_invalidate(nested_hwpt_id[1]); + test_cmd_hwpt_check_iotlb(nested_hwpt_id[1], 0); + /* Attach device to nested_hwpt_id[0] that then will be busy */ test_cmd_mock_domain_replace(self->stdev_id, nested_hwpt_id[0]); diff --git a/tools/testing/selftests/iommu/iommufd_utils.h b/tools/testing/selftests/iommu/iommufd_utils.h index 7d9efe3f7e0d..fef17fb46160 100644 --- a/tools/testing/selftests/iommu/iommufd_utils.h +++ b/tools/testing/selftests/iommu/iommufd_utils.h @@ -169,6 +169,26 @@ static int _test_cmd_hwpt_alloc_nested(int fd, __u32 device_id, __u32 parent_id, _test_cmd_hwpt_alloc_nested(self->fd, device_id, \ parent_id, hwpt_id)) +static int _test_cmd_hwpt_invalidate(int fd, __u32 hwpt_id) +{ + struct iommu_hwpt_invalidate_selftest data = { + .flags = IOMMU_TEST_INVALIDATE_ALL, + }; + struct iommu_hwpt_invalidate cmd = { + .size = sizeof(cmd), + .hwpt_id = hwpt_id, + .data_len = sizeof(data), + .data_uptr = (uint64_t)&data, + }; + + return ioctl(fd, IOMMU_HWPT_INVALIDATE, &cmd); +} + +#define test_cmd_hwpt_invalidate(hwpt_id) \ + ASSERT_EQ(0, _test_cmd_hwpt_invalidate(self->fd, hwpt_id)) +#define test_err_cmd_hwpt_invalidate(_errno, hwpt_id) \ + EXPECT_ERRNO(_errno, _test_cmd_hwpt_invalidate(self->fd, hwpt_id)) + static int _test_cmd_access_replace_ioas(int fd, __u32 access_id, unsigned int ioas_id) {