From patchwork Thu Mar 9 08:08:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 66666 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp165093wrd; Thu, 9 Mar 2023 00:17:38 -0800 (PST) X-Google-Smtp-Source: AK7set+JR5wf+6GCbpNdkaBkHK3dbZ6wjNqO3HS/Xr5cWc6ATmGnep6bQ9qJDap7HOwyFNYDpWNJ X-Received: by 2002:a17:902:d381:b0:19a:b033:2bb1 with SMTP id e1-20020a170902d38100b0019ab0332bb1mr18663986pld.44.1678349857919; Thu, 09 Mar 2023 00:17:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678349857; cv=none; d=google.com; s=arc-20160816; b=A+AzrBHCRv97Rpeuf/3Cf80BZc3CbWWo5gd49VjhkctT6c1cfmZ2HIzVaidmhWn+2C Mm221AtWBfck48oUViLjwclBdmIKKNsbOsU70Fv0lDhup5gRruIPpBoN2qKat2g8ErxP yg5ADPmSUD6+Ima6fS5DEZMQpeKyl0qAjAaPpmcxFIbEHIcu2kGIqG7X4NDJdnaF7CEp CL5AatwVrLIEuYdCNtZWBuUnFvvcJ3eaXK3FkNDX5AgIDInsshmFpsUJjs2sNmXCKWqv GDzMGER2gRLfStULXVPPSZFFH60eR1/JiVJOxy0bv/fXPXE+Jy79ATpw68PSwwDOoAP+ N4Eg== 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=LNXdthArayQPSQrt8i0NkPukWlcGM440DpIxKAnMwGA=; b=ov0tiYPuu+c86C2X7a6q6Ypsak1j0Jk60YkD8MjFLZ+OsirFoIXIZuIYzF0TaadEe2 O9lQfv8mVSPzoD090e35RrbkeNYMaskUW6V/JhmKMI5QSCsI0Kt08VOWCXfag3RZTyEu u8JFq6vazEZ6ynDnYgf4ZGhuI7ZgnZMWmNagl1H0/k0HjlIiz/1/VR/xRkoz6IqnwWZT q0gkcr6r5ZliD7496IH+hL01FJJRSJSNsIzWUT1KhAgPMRdWy26iCnHNb1bhhHh1Jp2P Z1ixpKDHZ6oggXiZ5RhyK/51u9SwAH3CPV3iH7ZLxiGsl8JGAK8KRmdYRy7o0jswnncL ap0Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=SuHuIuvY; 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 kr12-20020a170903080c00b00194a1f665b5si15860534plb.570.2023.03.09.00.17.25; Thu, 09 Mar 2023 00:17:37 -0800 (PST) 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=SuHuIuvY; 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 S230102AbjCIIJl (ORCPT + 99 others); Thu, 9 Mar 2023 03:09:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56730 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229549AbjCIIJd (ORCPT ); Thu, 9 Mar 2023 03:09:33 -0500 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3B2D273017; Thu, 9 Mar 2023 00:09:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1678349372; x=1709885372; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=U1VZYmCOLesVIRYg1SmVLfX+1m4uRnRI1+5TxmUm/rk=; b=SuHuIuvYtX5XwEeZxLplMCfAohMhhJxWJSTVLKggSmD6vSXLxaHrmh1z 9PZp7/6vGuT3SjwwOyWX/fvsZYOGjA2Lf8NaumVsZIzHnwzbT7i5hqAv+ LgZBU7hw1CfS0lh6Ba/VbEYHBsaNBgWFVVe6AGVz/+FuOtknx5ybjLCME 4NtfY3TPr3te2lHqn4VtzEUkoOQs51ZOILWd76CPaHOCloiA8qfE21PWZ Sp+CqfW4u1RotluzC495G1Kur5w8ZYYLka95uKgdlFcS5jdD5rjIJHRwz CBIIOHLMz/iYzKPiLLyLH7kgQ9MVYS8ayBqe+A3EKtiI6dzsxb1tbD8Qn A==; X-IronPort-AV: E=McAfee;i="6500,9779,10643"; a="364023029" X-IronPort-AV: E=Sophos;i="5.98,245,1673942400"; d="scan'208";a="364023029" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Mar 2023 00:09:23 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10643"; a="787471348" X-IronPort-AV: E=Sophos;i="5.98,245,1673942400"; d="scan'208";a="787471348" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga002.fm.intel.com with ESMTP; 09 Mar 2023 00:09:20 -0800 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 Subject: [PATCH 01/12] iommu: Add new iommu op to create domains owned by userspace Date: Thu, 9 Mar 2023 00:08:59 -0800 Message-Id: <20230309080910.607396-2-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230309080910.607396-1-yi.l.liu@intel.com> References: <20230309080910.607396-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 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?1759877380940118847?= X-GMAIL-MSGID: =?utf-8?q?1759877380940118847?= 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(). Suggested-by: Jason Gunthorpe Signed-off-by: Lu Baolu Signed-off-by: Yi Liu Reviewed-by: Jason Gunthorpe --- include/linux/iommu.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 3ef84ee359d2..a269bc62a31c 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -229,6 +229,7 @@ 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 * @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 @@ -266,6 +267,9 @@ 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 void *user_data); struct iommu_device *(*probe_device)(struct device *dev); void (*release_device)(struct device *dev); From patchwork Thu Mar 9 08:09:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 66673 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp167105wrd; Thu, 9 Mar 2023 00:24:14 -0800 (PST) X-Google-Smtp-Source: AK7set9i0z/QBpjKXThYAlnpiLGsLW59rh6B9DV9ZTsCu7oT5C36qw4wck+vxMT161AbcP9BAmBE X-Received: by 2002:a62:1905:0:b0:5a8:b6f3:a2a7 with SMTP id 5-20020a621905000000b005a8b6f3a2a7mr17319636pfz.11.1678350253959; Thu, 09 Mar 2023 00:24:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678350253; cv=none; d=google.com; s=arc-20160816; b=CvJBvbUcZbVp+LNBgahBWgmwznmbAbv81hGa7jo9EKu2JJOn6y/BHQgTEInTQQ3R3V m1Dez4qkBm1y4+IKltCzhpe/NXWhbhPJutBhadev0cZPmslc0+NPkxCvy9tAk6CCuNl2 OBxe3rAWmRy65ntxzoyOlRpzBwT0h4pmS7x8FqTnO0941Rm4MkQcQqWhPoJUkD710c/p I4GYQLWdza3y9r3pkM0GLIUi57QWnmNakpxHe9vzl9NRo6OeKueNmTBSFXuGR9aIpXE3 G4eK5o7VyrzvbVszC8vLi1wibHLuoovNvOpLY1waLmQHTCmfTSQifehCGlpUIktnH+Im OB7w== 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=qF+zXNuQFT14o7N1ZYbdGxVqh2qSNczHMFWpvCq3m7M=; b=SVOuXZvBKGaE709Qys0STentL0VFF9e5avuq5lTXQ+ew4p51qvLiRjY5208RFASKM1 sabS6hBbtEQH2wVcrulXe0PSjYp7M0vNNUNrYDvTxoR+QKCm9Bc3f8fJ0N6kdyGd4I7q 4P+312TnJfr31abRWNOLuWEm2LfgpgqZ6lZf5QKo63nCD47lHC+6ZufcNf446DH9Bg0y nevm3kEiPTrD4NkdJXGxdZZwPgadSH+MLD9lAnI2RZZTgfVCyLCstQRArKQsmLvCsc6c WOhIjug9KhO/4eva9pA5ltIbbQNrnZvXKLsHMLqiKtXrSnf4ni4gJHUfbLZqOlPK+dRF XCJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=lnwSDmgG; 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 n26-20020aa7985a000000b005e8a3dd45c3si17404675pfq.312.2023.03.09.00.24.01; Thu, 09 Mar 2023 00:24:13 -0800 (PST) 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=lnwSDmgG; 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 S230166AbjCIIJx (ORCPT + 99 others); Thu, 9 Mar 2023 03:09:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56728 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229910AbjCIIJd (ORCPT ); Thu, 9 Mar 2023 03:09:33 -0500 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1F42072036; Thu, 9 Mar 2023 00:09:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1678349372; x=1709885372; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=DK2k6evSa483gL5qczVDNJ/CshEmsXAsK8rNm81jnSk=; b=lnwSDmgGJIks8FzMKo9E4Qssf9G3eMpwOUyOXyIoPyx0l9RgM4YBixye YezXL3H52y2QvB8lQO3Eg6n6mfRa8ScazwsJ7I/OxkVcrm2S00aJv6/zA yEYsulrMY7gj2EZex1FoAa3uml5iAwPVHn6YXJTGVAFteReLBVJ28v5fJ JaKmLwxXQwpHcbcLEyMD0vZsQXdFGWRdVVKrRtYvn4YeaGglSI6d5MRG8 lQtcc44fNV8p3vRi/2bZAiW5Qe6O24CNtEErgTOSlxbFK1xZ3hs+FhV+T qN6cJqG5VV56QHvXf6dl1u2z8/2N2PxxaoHqheVdBJDvk8JAWBJGoi4QN A==; X-IronPort-AV: E=McAfee;i="6500,9779,10643"; a="364023019" X-IronPort-AV: E=Sophos;i="5.98,245,1673942400"; d="scan'208";a="364023019" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Mar 2023 00:09:23 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10643"; a="787471356" X-IronPort-AV: E=Sophos;i="5.98,245,1673942400"; d="scan'208";a="787471356" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga002.fm.intel.com with ESMTP; 09 Mar 2023 00:09:21 -0800 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 Subject: [PATCH 02/12] iommu: Add nested domain support Date: Thu, 9 Mar 2023 00:09:00 -0800 Message-Id: <20230309080910.607396-3-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230309080910.607396-1-yi.l.liu@intel.com> References: <20230309080910.607396-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 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?1759877795888186585?= X-GMAIL-MSGID: =?utf-8?q?1759877795888186585?= From: Lu Baolu Introduce a new domain type for a user space I/O address, 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 | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/include/linux/iommu.h b/include/linux/iommu.h index a269bc62a31c..080278c8154d 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -67,6 +67,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 * @@ -92,6 +95,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; @@ -325,6 +329,7 @@ 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 * @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 @@ -354,6 +359,8 @@ struct iommu_domain_ops { size_t size); void (*iotlb_sync)(struct iommu_domain *domain, struct iommu_iotlb_gather *iotlb_gather); + void (*cache_invalidate_user)(struct iommu_domain *domain, + void *user_data); phys_addr_t (*iova_to_phys)(struct iommu_domain *domain, dma_addr_t iova); From patchwork Thu Mar 9 08:09:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 66655 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp162645wrd; Thu, 9 Mar 2023 00:11:18 -0800 (PST) X-Google-Smtp-Source: AK7set+t/8lSWDW7/ei5Xv+FU5ndVA10E/myeYVVbIA+3YuEUs9wLedFG6WKR7+3NnoZmmekEV5u X-Received: by 2002:a17:902:bb90:b0:19e:524f:272f with SMTP id m16-20020a170902bb9000b0019e524f272fmr17550778pls.42.1678349478300; Thu, 09 Mar 2023 00:11:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678349478; cv=none; d=google.com; s=arc-20160816; b=LtRwQ7NOFqzDW+vsYr8Dfh8iyKNeZCfjX7pZOiObM5P8aQmF8HLnTf9z9F3Ey8XRtH bwRi/j5B5pF+xWAUkVjEsdLny0oD6ocO3AJZRKIsa4CEeKIXyW/UEQ8D4cXK5Lflzkyc RH9HU2xrWkyN1uLwmS8ZlsPJFT/nfUXX8YymS1D60cy7hzZtDq3eDueIui4AtHzuwYWU NE3KRp85SyCcQxTj/zIdsUHLMS26y4QmwmGZF8Q+aQyBa/d6Q1wHEr1DFTQZ2UhPH1X/ Hi49VRjlXnhS8mpcS2mDAPEis1FWRw+qnCUOpOP8QBcO1wr+aTIOJo6KBLSio4dY6zAo sqaQ== 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=jibbQrLk5cXwZxu1xj/MficgJ0Ecvw7jN47sd94NZcg=; b=tMzuwA1c4u1gHZWiScI9GtV7oIRzLe3NSMvJr6gl67GPtKby4dyFg2XzPlSpemlbJ/ rxnwrAG+YLwTzeowBdhBh/3/RM34IxWNbLxcvyWxlJMqvkbxAKUVaUes6rDPX3/wfMjF Sd3XgNKkOpvZzwlQV0hD4JLEJv4q4kgH1Sxx6CItglL3lOHXPBY3F9031gDlXQJigv4U nv6WN4Y6Sdz+eYDrUrMpYP+Dlpt+Wh4/4/XqiCCc20s8pwxoU3c8Q8GAq3FNFfecqmSS rCRPc+wmiQ9X4WODoAQ3MFTdzYJ8zW0nwv8ChFiFR7E/n9udwR9YedYlUrZdYeSEHC9Q j6Wg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="Zwu/Un9e"; 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 kd13-20020a17090313cd00b0019aa6c46413si16128860plb.345.2023.03.09.00.11.05; Thu, 09 Mar 2023 00:11:18 -0800 (PST) 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="Zwu/Un9e"; 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 S230195AbjCIIJ7 (ORCPT + 99 others); Thu, 9 Mar 2023 03:09:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56744 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229892AbjCIIJe (ORCPT ); Thu, 9 Mar 2023 03:09:34 -0500 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 332B26B97A; Thu, 9 Mar 2023 00:09:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1678349373; x=1709885373; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=aeM9wYEQtVfENpenMofCYElwE3QfNhdny2a0DT1ZPjw=; b=Zwu/Un9eZ/PU9ql5o9aoUcsQrGOU2lQUX4OU4NGXPYb5Qf/OEjsM8gIT vRydaVvDQoZmJnDq+a3qOCscM9WlJHQiNgarLVKtrQ6liNCaktMTfXW2/ 7w1pFNjZb3bb4ZiwghcKHB3aOZvGZmHucHVKr/aEDhz8ssf9skL48VZM8 U9xxR8j1HBYCDphiWbA2XMzMUekzn48sXq/4SRXgrs/2JYDTr3vWaa3GV DMqkvu8EWtMLcbHgUKpxVXhVejzE3Vb7ln0oMIpt59kq4kQAaMb3A+eXW 57E4grSXykZNlGvlbBwQ9tFvEbMSiUzZaN4rErQogc2f3nr/o3qziehKu w==; X-IronPort-AV: E=McAfee;i="6500,9779,10643"; a="364023033" X-IronPort-AV: E=Sophos;i="5.98,245,1673942400"; d="scan'208";a="364023033" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Mar 2023 00:09:24 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10643"; a="787471362" X-IronPort-AV: E=Sophos;i="5.98,245,1673942400"; d="scan'208";a="787471362" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga002.fm.intel.com with ESMTP; 09 Mar 2023 00:09:23 -0800 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 Subject: [PATCH 03/12] iommufd/hw_pagetable: Use domain_alloc_user op for domain allocation Date: Thu, 9 Mar 2023 00:09:01 -0800 Message-Id: <20230309080910.607396-4-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230309080910.607396-1-yi.l.liu@intel.com> References: <20230309080910.607396-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 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?1759876982657598750?= X-GMAIL-MSGID: =?utf-8?q?1759876982657598750?= This converts IOMMUFD to use iommu_domain_alloc_user() for iommu_domain creation. Suggested-by: Jason Gunthorpe Co-developed-by: Nicolin Chen Signed-off-by: Nicolin Chen Signed-off-by: Yi Liu Reviewed-by: Lu Baolu --- 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 13bdab4c801b..84b4a11e62f8 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) @@ -55,6 +56,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; @@ -69,7 +71,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 Mar 9 08:09:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 66662 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp163054wrd; Thu, 9 Mar 2023 00:12:28 -0800 (PST) X-Google-Smtp-Source: AK7set9Pt5I69JxQK5Jhhqv30JsDfUttlCfDxFNOok7qZaY0yfZJny6udNDu1hkLw3Z2tW96JfT8 X-Received: by 2002:a05:6a20:158a:b0:cc:a5e7:c40b with SMTP id h10-20020a056a20158a00b000cca5e7c40bmr24251164pzj.26.1678349548382; Thu, 09 Mar 2023 00:12:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678349548; cv=none; d=google.com; s=arc-20160816; b=CE32THE82xFc7eh0rog8GkkEmiLNBZojQlLar7w3ma7Mti4vAYbMp3125SeTclob0a 8Km29mXxuZK73jQTV5qvCsBXUbrjOEEoU23UCPHLKyKbanZc6Prs8gcocpw9jBVQkkkI QgfkhA/xvUiT3Ni5DAVVJcezyYpLvSdmXd6P1PJReWBVVgY4xEEpb7ZIfHX8DOVxEbRJ oEzhz25ODjUftg4O2Gfelly+Z2sY5Kyof/+dm8iKwezXarhudIeZEYaf63Mpj0zLW5WA kN+R9HolFOnENjU+YcNzseKjg6Lx3MOumTbXZ0bDf9QdRvG7xRITnvufwJwSoXwVFBqK 7nFQ== 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=zDigxAuQ+C1l95NvRXY389Ly4GlQ+aAyQq6Jw4ITNyg=; b=ehV/aG4IPhxd5XBjnS5p/INr4FiEwYZNxpAUbdAwDCMcHvrp+QQAeHoyQTVnyYnz+e LZx5zH1Ce1UFWVNvtdAqlp9jIImCOBRvDBEuMh/RyXJ0wnVb6bWR56LzDshT60H0CXT8 S/A6EidCST2g3ENl8rBG8a/qh5n+9SogguWT4sdMOATuetewYH1mWm6hUoOY0gvI3U6t SXleb1elja+XFGJvdEr6ozuHmoev0Ru+2r9kD3ASR3n24qHCiLCPLYq9idCnBLmmATTT Am6+ckC3Lm+vt1zXH1VVyysQFrllyUT+ghmq84SsJXp4qWkn+nEVe4u1AO/UsOCEq/4N Fq9Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=e1zPhXb3; 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 c4-20020a630d04000000b005077dda0499si3656988pgl.731.2023.03.09.00.12.16; Thu, 09 Mar 2023 00:12:28 -0800 (PST) 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=e1zPhXb3; 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 S229696AbjCIIKD (ORCPT + 99 others); Thu, 9 Mar 2023 03:10:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56754 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229928AbjCIIJe (ORCPT ); Thu, 9 Mar 2023 03:09:34 -0500 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4211870413; Thu, 9 Mar 2023 00:09:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1678349373; x=1709885373; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=f6a+QAIRGPkjHfuRSA0kkXfLfQHx0PbJp4L/PN4a0aE=; b=e1zPhXb3dOUV+Bc4QV8O8xkWuvAwq1UI8SZX+f2ek4w9POvOHmwvNxFo u9N15MXHF1q0vvDPqBgJqnNNo2D2oCBGrLoWIrMAunhAKllGzV5DaCo/c dMN738rDHXt+KKEWzX/Q2Fpf8jwlqYXxAQscD8dm2vkRkaCWn2CvLvU98 tS/aVbuw7qAH0qanQ9VZgLzRJhKnQUKwf2fH9JT0W+cYUROd18omT9CRG V23b0r7go2xRm/oIRhq3vlYrq5JacOTYzvQIvrnTCd9zVfLUnOhuENWmV uhrqi1CMu0EB3l8bsO6/+z2ECK0yqjwWCi03XtvTM1oM/pjtuQEDcBnRS Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10643"; a="364023048" X-IronPort-AV: E=Sophos;i="5.98,245,1673942400"; d="scan'208";a="364023048" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Mar 2023 00:09:25 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10643"; a="787471371" X-IronPort-AV: E=Sophos;i="5.98,245,1673942400"; d="scan'208";a="787471371" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga002.fm.intel.com with ESMTP; 09 Mar 2023 00:09:24 -0800 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 Subject: [PATCH 04/12] iommufd: Pass parent hwpt and user_data to iommufd_hw_pagetable_alloc() Date: Thu, 9 Mar 2023 00:09:02 -0800 Message-Id: <20230309080910.607396-5-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230309080910.607396-1-yi.l.liu@intel.com> References: <20230309080910.607396-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 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?1759877056191639915?= X-GMAIL-MSGID: =?utf-8?q?1759877056191639915?= 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 | 28 ++++++++++++++++++++++--- drivers/iommu/iommufd/iommufd_private.h | 5 ++++- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 5c352807d946..19cd6df46c6a 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -558,7 +558,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 84b4a11e62f8..16e92a1c150b 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); } @@ -46,6 +48,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 @@ -54,14 +58,20 @@ 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, + void *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; int rc; lockdep_assert_held(&ioas->mutex); + if (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; @@ -70,9 +80,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) { @@ -80,6 +96,11 @@ 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 */ + if ((parent_domain && hwpt->domain->type != IOMMU_DOMAIN_NESTED) || + (!parent_domain && hwpt->domain->type != IOMMU_DOMAIN_UNMANAGED)) + 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 @@ -150,7 +171,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); mutex_unlock(&ioas->mutex); if (IS_ERR(hwpt)) { rc = PTR_ERR(hwpt); diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 05b5ad66f716..182c074eecdc 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -243,6 +243,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 +255,9 @@ 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, + void *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 Mar 9 08:09:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 66661 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp163052wrd; Thu, 9 Mar 2023 00:12:28 -0800 (PST) X-Google-Smtp-Source: AK7set9OMylvarKDBKNrGTtoisfr2BFxnwnvh30Nk31xnnu+vQ1o1V4x0BL8d9zoihAJgO/KX/Xn X-Received: by 2002:a62:184d:0:b0:5e3:ba14:8566 with SMTP id 74-20020a62184d000000b005e3ba148566mr13443310pfy.28.1678349548089; Thu, 09 Mar 2023 00:12:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678349548; cv=none; d=google.com; s=arc-20160816; b=OEBvpxnodRCUnYJUi3ikjaG+gF+tM/M6DkMhtLqNKZMSnW+2PvXmkNk+EQxssVKQda tyW0Xx99eTpeACmZcfGQc7KPXQN+GNao0t5pplh4Paq+0g/kAjq13Q66I62UldWF8e/E vJLb62EFceNLvgmhrv10CwrkQHLcFNPfeRymYfxVz7XoDdRIwgcYcqWB9XxPXprXfoFG zvapwnOC2Uvp4n/XMmnN1OE++4+cdZ7ZZHan9/XNi7JmymhPCO6d6cpmAonHH9PW0BFp 1ketiijGnf4Dovl5WHeIVK1FbsiS9VcBC5jCTbOoTlI35mPtOjdKB1ncSVDMSNtCOG83 XAOw== 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=kzyH4dHkXLiAP7gbZYHNlR9e0Fiy4Cqnz2jAjZj1o3g=; b=KaJlQLUEyqNeDjmhYZgT0TRmgxmopOM7dOzzdOpJYO0KEycEQoMfDPYo9bihQlVWD5 STxX0W1bHY7E0pL3Q4/uOitrHQdzCu67LX6glJ02dwoSX4L6FfhPMNvw3mOII1KRE5Bv k4yBlukgsWa2NuxYkqGsIWLwLNe6UptTRA23b0lkulpBhbgfr9ZAcxOFxUA6IjtyUsA6 rspMgTn2eUPV8prtAZEeA67EOjjonvtNCasg+N5bV2mojgohlroPeiYna9Yv/Edy8TwO dphFhw+Gm6rkur7RAhnIG3+waqVu8sBONnz3RGSP536CzBoVRVz2/qZrP2Uw5K+vZVPm 8/JQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=gUaae1S1; 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 m2-20020a63fd42000000b004fcc0092a06si16322929pgj.663.2023.03.09.00.12.15; Thu, 09 Mar 2023 00:12:28 -0800 (PST) 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=gUaae1S1; 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 S230002AbjCIIKK (ORCPT + 99 others); Thu, 9 Mar 2023 03:10:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56762 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229943AbjCIIJe (ORCPT ); Thu, 9 Mar 2023 03:09:34 -0500 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 52F657302F; Thu, 9 Mar 2023 00:09:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1678349373; x=1709885373; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ze+YqyOq2FcrfezTmpCROik4OQxGxrLylWyUGP+0lZs=; b=gUaae1S1QdkkM12C3plJrp3gD35ZIuOuIl0wqjG6ppAaRe8HxEABnC2z Jrrfh64Az5sC4l+j3d7I2IRhNcn5OBwaykka1Z+/DwFzmDgrU0ZSiynWN 2yH0fsBwNf+zmALZFX42BtlOxbU6ChxFC+Z3pX2qs3xg7vu2YJwnbCH6L +9aYgtrFkHVyzCSheMdGJPfAyglR470sUSa9b6OsvOw/jqkFp1dg4atmm 3zkKTHG6ffNa0eocEt/o66AIdd9IDwTU1r6AIwkaFV6SvAKxasksubk7l wb8UHAwjwWl5kMELY1Q/iVI5vd5sZcv6RiB4ldIiaLf3lk5mOixZ70nRz w==; X-IronPort-AV: E=McAfee;i="6500,9779,10643"; a="364023072" X-IronPort-AV: E=Sophos;i="5.98,245,1673942400"; d="scan'208";a="364023072" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Mar 2023 00:09:29 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10643"; a="787471382" X-IronPort-AV: E=Sophos;i="5.98,245,1673942400"; d="scan'208";a="787471382" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga002.fm.intel.com with ESMTP; 09 Mar 2023 00:09:26 -0800 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 Subject: [PATCH 05/12] iommufd/hw_pagetable: Do not populate user-managed hw_pagetables Date: Thu, 9 Mar 2023 00:09:03 -0800 Message-Id: <20230309080910.607396-6-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230309080910.607396-1-yi.l.liu@intel.com> References: <20230309080910.607396-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 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?1759877055678548291?= X-GMAIL-MSGID: =?utf-8?q?1759877055678548291?= 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 | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index 16e92a1c150b..6e45ec0a66fa 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -43,6 +43,23 @@ 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; + + if (hwpt->parent) + hwpt = hwpt->parent; + + if (!list_empty(&hwpt->hwpt_item)) + 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 @@ -131,10 +148,9 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, goto out_unlock; } - 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); mutex_unlock(&idev->igroup->lock); return hwpt; From patchwork Thu Mar 9 08:09:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 66665 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp164881wrd; Thu, 9 Mar 2023 00:17:02 -0800 (PST) X-Google-Smtp-Source: AK7set/4BCw9yZg86ZcEKe6e7Oo+MgAxqeYwVIJL39TLXsDSCV2Ydlr8LVfW+sSAPvlKcgKIUMgw X-Received: by 2002:a17:90b:4b41:b0:234:f4a:8985 with SMTP id mi1-20020a17090b4b4100b002340f4a8985mr22318756pjb.15.1678349822134; Thu, 09 Mar 2023 00:17:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678349822; cv=none; d=google.com; s=arc-20160816; b=yB0ibX0JnLfViaMniRHv3LqUn+hlp978yPItJQpMBiiJa4VQ+Q4FRHJ4VUN2paVBH4 6rHaACVuaxZAdhNvbHG2KrMSIjDRkSecMa7RF82mfmlrcX1n0VB/st3KcxEqWwSVR2Mk qfjbaX3djOEtmrg/QZuZKYZm1v0w7COnHNnmBpKKjN6Qxbmymcuffrbcwo8QQ0QqB90H fNi4JVPRW9hw+A6slw2hVbwzY3oFhKSu+uFuEo3ai/b3jwyHIELLbCCM1G1zin09hVop c4BtIHeaHwRwLk6pp5OWRCYJbXjgbXEbg8WqtiFkDg0wtDL+xLAcwCKIEhQdEuqMwBrB hzUA== 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=7JPX5lWn0Hie/LOwfMkIg7/c5bJvZg9tXEmL2IkSBcI=; b=H5ifcXo6qIiBCCGUVIT80LYg/gqke5zhGeDbD98fvoubHBFEP+xN+U7yzICZIE3iP1 z5myiYGmW/C09BTsjlNqflvAt4ooptNEa8o3ByRKJSiz7gCwS5Ai8es3cJXQH/GgAg+I Te1e7azJjPwtYI+vFh5llKA9ATnzkdtqxDD5pGZuaHCBxtqvcfps1qENPmYIpaNcRCxO zsIJMhsaCHBuOg7aRYGLwtzrBaIvFMLPAaqzhIShbxAGo/NEeoRVPulMAxke+i8fwnxU 5MRM0mdrChkGCMQvp2Xj0D/l+D9fI7DCrZrX1rp1EjIHOWBIoK6ZcnCe3JzkD9I8LAKD q2uw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=kySjMN45; 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 ob3-20020a17090b390300b00234bc771801si1754574pjb.57.2023.03.09.00.16.49; Thu, 09 Mar 2023 00:17:02 -0800 (PST) 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=kySjMN45; 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 S229476AbjCIIKN (ORCPT + 99 others); Thu, 9 Mar 2023 03:10:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56794 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229958AbjCIIJf (ORCPT ); Thu, 9 Mar 2023 03:09:35 -0500 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0325173035; Thu, 9 Mar 2023 00:09:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1678349374; x=1709885374; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=KTRiWPVEMnAblPTuUj5ebCwkle4aIieP4ZkxIOdMmhM=; b=kySjMN45PYRPcBCJD544N5ICv4VqXNp05kzAsctkBCyfYpg6KlGnaVda AlKIFvbdFA8NPqQEKG2KaAnBjCxiyMVttC7dMJTGkz51BkxmYH/KiDlLp i965Tn5g8d7nMY8nkf42CQ3vskUPouSy9qqtJmXuzMRPP0OMKHIKaTVtp a9Tdzz/92YyBhx0IDRZFRX1oe0/rtrWmcKSLLuk6xgZjBCikbrwqQzlEK cP08JY20fJde118y9JcWDLp8h83UrlkOcj6Stx3dkQlOIaPWIZoPOhv7n 22/qZMb/RV5vHuIM03EO338UyJpf3bwDzNhJT6A49jTDT8lGo24l5qrW2 w==; X-IronPort-AV: E=McAfee;i="6500,9779,10643"; a="364023083" X-IronPort-AV: E=Sophos;i="5.98,245,1673942400"; d="scan'208";a="364023083" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Mar 2023 00:09:29 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10643"; a="787471385" X-IronPort-AV: E=Sophos;i="5.98,245,1673942400"; d="scan'208";a="787471385" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga002.fm.intel.com with ESMTP; 09 Mar 2023 00:09:29 -0800 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 Subject: [PATCH 06/12] iommufd: IOMMU_HWPT_ALLOC allocation with user data Date: Thu, 9 Mar 2023 00:09:04 -0800 Message-Id: <20230309080910.607396-7-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230309080910.607396-1-yi.l.liu@intel.com> References: <20230309080910.607396-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 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?1759877343130929309?= X-GMAIL-MSGID: =?utf-8?q?1759877343130929309?= 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 | 94 +++++++++++++++++++++++++--- drivers/iommu/iommufd/main.c | 2 +- include/uapi/linux/iommufd.h | 30 +++++++++ 3 files changed, 115 insertions(+), 11 deletions(-) diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index 6e45ec0a66fa..64e7cf7142e1 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -165,34 +165,106 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, return ERR_PTR(rc); } +/* + * size of page table type specific data, indexed by + * enum iommu_hwpt_type. + */ +static const size_t iommufd_hwpt_alloc_data_size[] = { + [IOMMU_HWPT_TYPE_DEFAULT] = 0, +}; + int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd) { struct iommu_hwpt_alloc *cmd = ucmd->cmd; - struct iommufd_hw_pagetable *hwpt; + struct iommufd_hw_pagetable *hwpt, *parent = NULL; + struct iommufd_object *pt_obj; struct iommufd_device *idev; struct iommufd_ioas *ioas; + const struct iommu_ops *ops; + void *data = NULL; + u32 klen; int rc; - if (cmd->flags) + if (cmd->__reserved || cmd->flags) return -EOPNOTSUPP; idev = iommufd_get_device(ucmd, cmd->dev_id); if (IS_ERR(idev)) return PTR_ERR(idev); - ioas = iommufd_get_ioas(ucmd, cmd->pt_id); - if (IS_ERR(ioas)) { - rc = PTR_ERR(ioas); + ops = dev_iommu_ops(idev->dev); + if (!ops) { + rc = -EOPNOTSUPP; goto out_put_idev; } + /* Only support IOMMU_HWPT_TYPE_DEFAULT for now */ + if (cmd->data_type != IOMMU_HWPT_TYPE_DEFAULT) { + rc = -EINVAL; + goto out_put_idev; + } + + 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 data_type is !IOMMU_HWPT_TYPE_DEFAULT */ + if (cmd->data_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; + } + + klen = iommufd_hwpt_alloc_data_size[cmd->data_type]; + if (klen) { + if (!cmd->data_len) { + rc = -EINVAL; + goto out_put_pt; + } + + data = kzalloc(klen, GFP_KERNEL); + if (!data) { + rc = -ENOMEM; + goto out_put_pt; + } + + rc = copy_struct_from_user(data, klen, + 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); mutex_unlock(&ioas->mutex); if (IS_ERR(hwpt)) { rc = PTR_ERR(hwpt); - goto out_put_ioas; + goto out_free_data; } cmd->out_hwpt_id = hwpt->obj.id; @@ -200,12 +272,14 @@ int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd) if (rc) goto out_hwpt; iommufd_object_finalize(ucmd->ictx, &hwpt->obj); - goto out_put_ioas; + goto out_free_data; out_hwpt: iommufd_object_abort_and_destroy(ucmd->ictx, &hwpt->obj); -out_put_ioas: - 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 f079c0bda46b..7ab1e2c638a1 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -295,7 +295,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 4ac525897b82..48781ff40a37 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 + * @data_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 normal HWPT will be created with the mappings from the given IOAS. + * The @data_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 @data_type must not be set to IOMMU_HWPT_TYPE_DEFAULT but a + * pre-defined type corresponding to the underlying IOMMU hardware. + * + * If the @data_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. + * + * +==============================+=====================================+===========+ + * | @data_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 data_type; + __u32 data_len; + __aligned_u64 data_uptr; }; #define IOMMU_HWPT_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HWPT_ALLOC) From patchwork Thu Mar 9 08:09:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 66657 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp162718wrd; Thu, 9 Mar 2023 00:11:27 -0800 (PST) X-Google-Smtp-Source: AK7set8ictn6+NlTAc6nSsleKX8gfU+bfhhAMWpwWiMzluXOjfPXCZcA0q5rvhUqRh/tMmrromLa X-Received: by 2002:a17:902:ba83:b0:19c:d6fe:39cb with SMTP id k3-20020a170902ba8300b0019cd6fe39cbmr17798897pls.7.1678349487344; Thu, 09 Mar 2023 00:11:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678349487; cv=none; d=google.com; s=arc-20160816; b=vQSWpzS3cvYFjA/Ej8T0UbC+jndkhl30Jq+bfHJbR6+T0gnbDLGZdhaOCwp7PkJuF+ rkwhhY1OC4fUz2R2Kj0ptVzC3jcwm+WdBb+Td7h/w3DZ1oJ0v5I0n25mhKbQN3t1BEpK qTxEdHfVo54hd3cz1UStGfeSEXdRIuncLNI5SNi3+j7Hk1tiC/jTqw3DwLrbKJ1Mp5W3 eMYaYSdc4HxbGPfkV6BxjcWRgwQQJESJc/smotAptcLMvjNLFKKB8O/P/NzaUSgIjZk0 0Y1MH5skzRmyPtlxhCNURegXrZJgfxFKAZ5NuH6ZhI/XKohqczFHnj27E2QxVjtyMJMA VQGA== 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=43qZV7IwoUv8jIsSdRcfCayIlKjJVdY1vnwrfBQoykU=; b=dApbP5HHLUu3U6Q1wsDAHEOl+bAT8ohWBgJw9DUILwXlnOUzmY8b6RQ4uUQw/CrPzE +Kagcw64wk6YyCEF4ULR7jg898deqWgvx4c1vrwWwagTw7NAIXg1d6UwcNkQAMwb6Vs7 05j58sTVe6WjWm2sbXafeIIyV2AmJZ20srQvjwlECONvQQgiG/RflURNzZDU4ZP4LWKA xwkrmmuGGHWMQvqfjzfDXbokmgyKv9S5d5Enav+K6UEz5FW170sCkJTBteaRQZdEfgfP cfG0Qy+zCazg04eQHmKEEZ//HfBB6ID7/hHFjFwJ/bPbPW+Fsh05EHr6kNoxAKoNh5E9 cN+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=EdI+WVFt; 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 w14-20020a170902d70e00b0019ca3e987dbsi15829030ply.631.2023.03.09.00.11.15; Thu, 09 Mar 2023 00:11:27 -0800 (PST) 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=EdI+WVFt; 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 S230142AbjCIIKW (ORCPT + 99 others); Thu, 9 Mar 2023 03:10:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56992 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230078AbjCIIJk (ORCPT ); Thu, 9 Mar 2023 03:09:40 -0500 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 44C6B7F00A; Thu, 9 Mar 2023 00:09:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1678349379; x=1709885379; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=j/S4apaXyrmximWwHCUE+Zj/DjMfF+CJS1FfehESmPI=; b=EdI+WVFtTEflbRP3undErShgH9qHqhO2H76ulix3W5s0kjArTpKm8B33 1wzxqeNwly2GadETrupWxbhGK5uaj4pq3C6x8zSQCyEEDhHv70oNOXqgg +Mwvpx2mUYxzfT27dohJ5CX53fSPCa8zRM8vSOp4uwwLNxiB3i/GH/aNW +DRGI/yUA+nNlCBMhrfAXphIitziNhPTSmJw7KBbqiddgt4yZ/rnqHAHh DWzECE4t50b3mGMzRZYQ2m/yAZO/qmxwyEVdNCvnO9a5I6ve8ynqtnwgs 9Y8cmFRLfex3rHYJbYqE6CbOg325Jzg16eFscnwzZexmKCJqjca1u5mZE w==; X-IronPort-AV: E=McAfee;i="6500,9779,10643"; a="364023116" X-IronPort-AV: E=Sophos;i="5.98,245,1673942400"; d="scan'208";a="364023116" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Mar 2023 00:09:38 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10643"; a="787471389" X-IronPort-AV: E=Sophos;i="5.98,245,1673942400"; d="scan'208";a="787471389" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga002.fm.intel.com with ESMTP; 09 Mar 2023 00:09:30 -0800 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 Subject: [PATCH 07/12] iommufd: Add IOMMU_HWPT_INVALIDATE Date: Thu, 9 Mar 2023 00:09:05 -0800 Message-Id: <20230309080910.607396-8-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230309080910.607396-1-yi.l.liu@intel.com> References: <20230309080910.607396-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 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?1759876992281793366?= X-GMAIL-MSGID: =?utf-8?q?1759876992281793366?= In nested translation, the stage-1 page table is user-managed and used by IOMMU hardware, so destroying mappings 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 | 56 +++++++++++++++++++++++++ drivers/iommu/iommufd/iommufd_private.h | 9 ++++ drivers/iommu/iommufd/main.c | 3 ++ include/uapi/linux/iommufd.h | 27 ++++++++++++ 4 files changed, 95 insertions(+) diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index 64e7cf7142e1..67facca98de1 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -284,3 +284,59 @@ int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd) iommufd_put_object(&idev->obj); return rc; } + +/* + * size of page table type specific invalidate_info, indexed by + * enum iommu_hwpt_type. + */ +static const size_t iommufd_hwpt_invalidate_info_size[] = {}; + +int iommufd_hwpt_invalidate(struct iommufd_ucmd *ucmd) +{ + struct iommu_hwpt_invalidate *cmd = ucmd->cmd; + struct iommufd_hw_pagetable *hwpt; + u64 user_ptr; + u32 user_data_len, klen; + int rc = 0; + + /* + * For a user-managed HWPT, type should not be IOMMU_HWPT_TYPE_DEFAULT. + * data_len should not exceed the size of iommufd_invalidate_buffer. + */ + if (cmd->data_type == IOMMU_HWPT_TYPE_DEFAULT || !cmd->data_len || + cmd->data_type >= ARRAY_SIZE(iommufd_hwpt_invalidate_info_size)) + 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 = iommufd_hwpt_invalidate_info_size[cmd->data_type]; + if (!klen) { + rc = -EINVAL; + 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; + + 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 182c074eecdc..d879264d1acf 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -265,6 +265,7 @@ struct iommufd_hw_pagetable * iommufd_hw_pagetable_detach(struct iommufd_device *idev); void iommufd_hw_pagetable_destroy(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) @@ -276,6 +277,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 7ab1e2c638a1..2cf45f65b637 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -263,6 +263,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; @@ -298,6 +299,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 48781ff40a37..d0962c41f8d6 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, }; /** @@ -447,4 +448,30 @@ 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_type: One of enum iommu_hwpt_type + * @data_len: Length of the type specific data + * @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. This is only needed by user-managed hw_pagetables, so + * the @data_type should never be IOMMU_HWPT_TYPE_DEFAULT. + * + * +==============================+========================================+ + * | @data_type | Data structure in @data_uptr | + * +------------------------------+----------------------------------------+ + */ +struct iommu_hwpt_invalidate { + __u32 size; + __u32 hwpt_id; + __u32 data_type; + __u32 data_len; + __aligned_u64 data_uptr; +}; +#define IOMMU_HWPT_INVALIDATE _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HWPT_INVALIDATE) #endif From patchwork Thu Mar 9 08:09:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 66658 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp162733wrd; Thu, 9 Mar 2023 00:11:29 -0800 (PST) X-Google-Smtp-Source: AK7set/16euXMv870DiA5LDwDUvGfDAh8ljtZmAldlyd+P/htkx/J5EnpjhmmfuEikNoRicdlBIy X-Received: by 2002:a17:903:32c7:b0:19c:be49:48e1 with SMTP id i7-20020a17090332c700b0019cbe4948e1mr25267953plr.7.1678349489544; Thu, 09 Mar 2023 00:11:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678349489; cv=none; d=google.com; s=arc-20160816; b=Inb4LPzbFXsKu4A0MqJ8ezVRkTWTlJbFHg5nODg9oo6pCMH6rastJRgionD6EiXNVS L6tAmIoj4Hp9jmgAgXGJCLxGLC1Y7IeFqiA6cmZuRRoqktTUxYIKmcOwSQBh4JVhMkYV PksAqlAROU1ltjMFMI0ah3xZJh27doU65zxRUbcotUD1UiBfecgKVucezrQD/Hw7vrbu OeEgGmNlCnPa4qhc60bT3UFzxZX8aGKDj9pu+CNHJ9PTyIQ5k2vbQxiuxgmyo8OVEdEo eC7gEIA7qxNWpr7fn/FZGJTDISVgepfIjkZVx12k6AZqhnQ3C36w2YYNXWQiVKAdQoJx CDOA== 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=QvamXsx+9pIElyqngXA6tJkiKUSQ68ZdFOJTei9u/04=; b=NFa0zIAOSu8DMCRMsnSsPP2yziSaV2PaznVBjtEcq5g1KI58p905+9qaeTZLRKH6Mu 7/LsMndTmWtGYHpfk0oQCRM6wc0EP6xYTY2JeSo/1It2PPA6He4Bwv41gA7QpH9RdiJx 2zApT8MJjpVv6znK2FcUHEfiqzc4VwV/BM/12aIHC4OfIjCmy5MBtlwZx6XHPcTJHJ5u sHMA7MsPlqfUFeIbce8Y4Ni4lXBp/OxCXlVkFGqGgiQ2KgTuB3D9GzYr4DtakYi7dJpI S3Rb1myu5ZUSQerYPcsQTKxljalnQwRwNct5w3p43GRqeCiZ0FmWIj/uUxrTaaS7rSFa 6FoA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="gB//xKSL"; 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 z3-20020a170903018300b0019adf50729fsi17606490plg.11.2023.03.09.00.11.16; Thu, 09 Mar 2023 00:11:29 -0800 (PST) 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="gB//xKSL"; 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 S230174AbjCIIK3 (ORCPT + 99 others); Thu, 9 Mar 2023 03:10:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56994 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230092AbjCIIJk (ORCPT ); Thu, 9 Mar 2023 03:09:40 -0500 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B0B28888AC; Thu, 9 Mar 2023 00:09:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1678349379; x=1709885379; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Z7V/YOwn030poZpCpEChyw8MC+7YqADB7tm3JIqLGIM=; b=gB//xKSLLn5/juX7z0zPe9bJIFF8HtlM0FR/zPGydo8JxtnLppIdxJbI dNOw8twirYUQg5m27vHPFgF1+HZiwWEW0wVUNq4oqGfWZsZ5mRyFlHbrg KtSlk1srLc1KvotH/tIISpzB1WF9NMn4i8QgHhHLuLO6MZHuOTqngIyvC ZaOPGsqFQBF83xVAs2FFPC3cKmx3mwTgf2IULEShAoAiDCG+3091olMul qVTAvW6UGT85nQGnYEkxSM37JmITK2hj5x5pLbwxTphyRRJtoOpv+wmVG Wjo42jF1RAh9zUlpjWCMXAefxaf2ud6LBp9OqVLVnwBBnEn8YpCDxlUdg g==; X-IronPort-AV: E=McAfee;i="6500,9779,10643"; a="364023131" X-IronPort-AV: E=Sophos;i="5.98,245,1673942400"; d="scan'208";a="364023131" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Mar 2023 00:09:38 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10643"; a="787471396" X-IronPort-AV: E=Sophos;i="5.98,245,1673942400"; d="scan'208";a="787471396" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga002.fm.intel.com with ESMTP; 09 Mar 2023 00:09:31 -0800 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 Subject: [PATCH 08/12] iommufd/device: Report supported hwpt_types Date: Thu, 9 Mar 2023 00:09:06 -0800 Message-Id: <20230309080910.607396-9-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230309080910.607396-1-yi.l.liu@intel.com> References: <20230309080910.607396-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 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?1759876994342675626?= X-GMAIL-MSGID: =?utf-8?q?1759876994342675626?= This provides a way for userspace to probe the supported hwpt data types by kernel. Currently, kernel only supports IOMMU_HWPT_TYPE_DEFAULT, new types would be added per vendor drivers' extension. Userspace that wants to allocate hw_pagetable with user data should check this. While for the allocation without user data, no need for it. It is supported by default. Co-developed-by: Nicolin Chen Signed-off-by: Nicolin Chen Signed-off-by: Yi Liu --- drivers/iommu/iommufd/device.c | 1 + drivers/iommu/iommufd/hw_pagetable.c | 18 +++++++++++++++--- drivers/iommu/iommufd/iommufd_private.h | 2 ++ drivers/iommu/iommufd/main.c | 2 +- include/uapi/linux/iommufd.h | 8 ++++++++ 5 files changed, 27 insertions(+), 4 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 19cd6df46c6a..0328071dcac1 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -322,6 +322,7 @@ int iommufd_device_get_hw_info(struct iommufd_ucmd *ucmd) cmd->out_data_type = ops->driver_type; cmd->data_len = length; + cmd->out_hwpt_type_bitmap = iommufd_hwpt_type_bitmaps[ops->driver_type]; rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd)); diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index 67facca98de1..160712256c64 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -173,6 +173,14 @@ static const size_t iommufd_hwpt_alloc_data_size[] = { [IOMMU_HWPT_TYPE_DEFAULT] = 0, }; +/* + * bitmaps of supported hwpt types of by underlying iommu, indexed + * by ops->driver_type which is one of enum iommu_hw_info_type. + */ +const u64 iommufd_hwpt_type_bitmaps[] = { + [IOMMU_HW_INFO_TYPE_DEFAULT] = BIT_ULL(IOMMU_HWPT_TYPE_DEFAULT), +}; + int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd) { struct iommu_hwpt_alloc *cmd = ucmd->cmd; @@ -182,7 +190,7 @@ int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd) struct iommufd_ioas *ioas; const struct iommu_ops *ops; void *data = NULL; - u32 klen; + u32 driver_type, klen; int rc; if (cmd->__reserved || cmd->flags) @@ -198,8 +206,12 @@ int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd) goto out_put_idev; } - /* Only support IOMMU_HWPT_TYPE_DEFAULT for now */ - if (cmd->data_type != IOMMU_HWPT_TYPE_DEFAULT) { + driver_type = ops->driver_type; + + /* data_type should be a supported type by the driver */ + if (WARN_ON(driver_type >= ARRAY_SIZE(iommufd_hwpt_type_bitmaps)) || + !((1 << cmd->data_type) & + iommufd_hwpt_type_bitmaps[driver_type])) { rc = -EINVAL; goto out_put_idev; } diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index d879264d1acf..164ccfc2e6e0 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -321,6 +321,8 @@ iommufd_get_device(struct iommufd_ucmd *ucmd, u32 id) void iommufd_device_destroy(struct iommufd_object *obj); int iommufd_device_get_hw_info(struct iommufd_ucmd *ucmd); +extern const u64 iommufd_hwpt_type_bitmaps[]; + struct iommufd_access { struct iommufd_object obj; struct iommufd_ctx *ictx; diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index 2cf45f65b637..7ec3ceac01b3 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -298,7 +298,7 @@ static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = { IOCTL_OP(IOMMU_HWPT_ALLOC, iommufd_hwpt_alloc, struct iommu_hwpt_alloc, data_uptr), IOCTL_OP(IOMMU_DEVICE_GET_HW_INFO, iommufd_device_get_hw_info, - struct iommu_hw_info, __reserved), + struct iommu_hw_info, out_hwpt_type_bitmap), IOCTL_OP(IOMMU_HWPT_INVALIDATE, iommufd_hwpt_invalidate, struct iommu_hwpt_invalidate, data_uptr), IOCTL_OP(IOMMU_IOAS_ALLOC, iommufd_ioas_alloc_ioctl, diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index d0962c41f8d6..e2eff9c56ab3 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -421,6 +421,8 @@ enum iommu_hw_info_type { * @out_data_type: Output the iommu hardware info type, it is one of * enum iommu_hw_info_type. * @__reserved: Must be 0 + * @out_hwpt_type_bitmap: Output the supported page table type. Each + * bit is defined in enum iommu_hwpt_type. * * Query the hardware iommu information for given device which has been * bound to iommufd. @data_len is the size of the buffer which captures @@ -435,6 +437,11 @@ enum iommu_hw_info_type { * The @out_data_type will be filled if the ioctl succeeds. It would * be used to decode the data filled in the buffer pointed by @data_ptr. * + * @out_hwpt_type_bitmap reports the supported hwpt types. This differs + * per the @out_data_type. Userspace should check it before allocating a + * user-managed hw_pagetable with user data, unless it allocates a default + * hw_pagetable that does not need user data. + * * This is only available for the physical devices bound to iommufd as * only physical devices can have hardware IOMMU. */ @@ -446,6 +453,7 @@ struct iommu_hw_info { __aligned_u64 data_ptr; __u32 out_data_type; __u32 __reserved; + __aligned_u64 out_hwpt_type_bitmap; }; #define IOMMU_DEVICE_GET_HW_INFO _IO(IOMMUFD_TYPE, IOMMUFD_CMD_DEVICE_GET_HW_INFO) From patchwork Thu Mar 9 08:09:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 66667 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp165102wrd; Thu, 9 Mar 2023 00:17:40 -0800 (PST) X-Google-Smtp-Source: AK7set8p+fgmHLkYuaJlly2K7Y7mFNIDSqPmTNMrmqJIeHabpKzUE3mHxsjE1leJcubDX/vEaQan X-Received: by 2002:a17:90b:1b0c:b0:237:cfdb:c251 with SMTP id nu12-20020a17090b1b0c00b00237cfdbc251mr20457578pjb.16.1678349860299; Thu, 09 Mar 2023 00:17:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678349860; cv=none; d=google.com; s=arc-20160816; b=AOpMHdPMeZEgfIXts+vf5s1uj5QexlFQB8rMn6J4bD5gufLzbjkwHsgdPfd/NHRO9u yWYpgMpD2LksNHa/Qr68rXtsQH+CawsVL7b9PfsSASEGWJ8Y2YliL4cQl1AdvZECXiNZ EwdFeb9lzipaSnkP4u1lLfF2vlyFxvHyYVgAaGFsNJHHaG0qs8E3sNlaHf1OUosAHnyl 7qDvGig376WGtpf/8bdm+8MrDsvfv5avrIpL6LWer1NIjGxdweLTksOECxhnwXl59Utk euqTubTv0+H3tABdFUoavRvoDgn8wYSAoh0HNHJ4WWLkQADI1xOyvpHO7lYDEz6OrJvM r6yg== 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=oWC66FGyFnPt3469yArZvYGorPvIBJc1RKiiDGYunDo=; b=WizIWADHMQHfpU2V9mRgFrQVS9CeTjCHIUNDF7yJW7mLmwOLdzKaw9SFDC7jgifXpL V0dctnv4ptoHiZ2YK4m5gp1X8atft3WeuxUUxwvILhwc5+jNtJuhoAYyLZ5dJhWmYb77 GMH9xyEqueW0xnSPR4y6JBV3vbx2+PsF6P8oUPsguXOL7bVst5Mz6yFtlCOyC1lrh5x6 Xi7UI2YSKRRuWYJ4CaCZHj4wLyGmibHRc3WoSHPnX3HDKNubAANnuFWcpGV4WiCpSEz9 ZsaK1Q6SI9z4LxmjME3C777HNpQQlGcQV7c9aId9X3ymwyNZitHldcpWaKbqXdwoLXay 3NWg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=HQsFQEb1; 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 kf6-20020a17090305c600b0019e2c25d670si15768318plb.572.2023.03.09.00.17.26; Thu, 09 Mar 2023 00:17:40 -0800 (PST) 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=HQsFQEb1; 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 S230340AbjCIILd (ORCPT + 99 others); Thu, 9 Mar 2023 03:11:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59266 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229580AbjCIIKj (ORCPT ); Thu, 9 Mar 2023 03:10:39 -0500 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 258E473035; Thu, 9 Mar 2023 00:10:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1678349401; x=1709885401; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=4TvI1XHiBlsBZES0NWLs08icFspIsJBbCUEMM8KMpcU=; b=HQsFQEb1fE21nbqHxXRQrwpmM4KusZ0OJ4EsrOCiTTBaGM8I/LtfRpgj oXAdLaOrykvmeyB5MXA5QlwleY9+61AuLsaCODeOyqYCTU68WIOq9zN8w +8wOUZOf3WQ6HMHWEq8S6CZUzcOmXsKi4NZhtzgRl/HxrUjjoqsQxJFgJ shAOUb8f8hnOizc3UHPolvrlPhvXPpJIR9PjGkeAd4oqiQYl6MVXcIszw /2P2ZlPtk2mqlOoA9MTjfcAV61QNUQi0NB/a9p6Sos/Bzt96pCYxW+wTp qQWPPK0O7FGRs/o9sHM6gCTHLmphd6iLPm97Pi9c51SLLyJGcA10BBew6 g==; X-IronPort-AV: E=McAfee;i="6500,9779,10643"; a="364023237" X-IronPort-AV: E=Sophos;i="5.98,245,1673942400"; d="scan'208";a="364023237" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Mar 2023 00:09:55 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10643"; a="787471421" X-IronPort-AV: E=Sophos;i="5.98,245,1673942400"; d="scan'208";a="787471421" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga002.fm.intel.com with ESMTP; 09 Mar 2023 00:09:42 -0800 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 Subject: [PATCH 09/12] iommufd/selftest: Add domain_alloc_user() support in iommu mock Date: Thu, 9 Mar 2023 00:09:07 -0800 Message-Id: <20230309080910.607396-10-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230309080910.607396-1-yi.l.liu@intel.com> References: <20230309080910.607396-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 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?1759877383080977827?= X-GMAIL-MSGID: =?utf-8?q?1759877383080977827?= 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/device.c | 8 +++- drivers/iommu/iommufd/hw_pagetable.c | 31 +++++++++++-- drivers/iommu/iommufd/iommufd_test.h | 16 +++++++ drivers/iommu/iommufd/selftest.c | 67 ++++++++++++++++++++++++---- 4 files changed, 109 insertions(+), 13 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 0328071dcac1..f95b558f5e95 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -322,7 +322,13 @@ int iommufd_device_get_hw_info(struct iommufd_ucmd *ucmd) cmd->out_data_type = ops->driver_type; cmd->data_len = length; - cmd->out_hwpt_type_bitmap = iommufd_hwpt_type_bitmaps[ops->driver_type]; + + if (ops->driver_type != IOMMU_HW_INFO_TYPE_SELFTEST) + cmd->out_hwpt_type_bitmap = iommufd_hwpt_type_bitmaps[ops->driver_type]; +#ifdef CONFIG_IOMMUFD_TEST + else + cmd->out_hwpt_type_bitmap = U64_MAX; // Pretend to support all types +#endif rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd)); diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index 160712256c64..0871da848447 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) { @@ -181,6 +182,18 @@ const u64 iommufd_hwpt_type_bitmaps[] = { [IOMMU_HW_INFO_TYPE_DEFAULT] = BIT_ULL(IOMMU_HWPT_TYPE_DEFAULT), }; +/* Return true if type is supported, otherwise false */ +static inline bool +iommufd_hwpt_type_check(enum iommu_hw_info_type driver_type, + enum iommu_hwpt_type type) +{ + if (WARN_ON(driver_type >= ARRAY_SIZE(iommufd_hwpt_type_bitmaps))) + return false; + + return ((1 << type) & + iommufd_hwpt_type_bitmaps[driver_type]); +} + int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd) { struct iommu_hwpt_alloc *cmd = ucmd->cmd; @@ -191,6 +204,7 @@ int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd) const struct iommu_ops *ops; void *data = NULL; u32 driver_type, klen; + bool support; int rc; if (cmd->__reserved || cmd->flags) @@ -209,9 +223,13 @@ int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd) driver_type = ops->driver_type; /* data_type should be a supported type by the driver */ - if (WARN_ON(driver_type >= ARRAY_SIZE(iommufd_hwpt_type_bitmaps)) || - !((1 << cmd->data_type) & - iommufd_hwpt_type_bitmaps[driver_type])) { + if (driver_type != IOMMU_HW_INFO_TYPE_SELFTEST) + support = iommufd_hwpt_type_check(driver_type, cmd->data_type); +#ifdef CONFIG_IOMMUFD_TEST + else + support = true; /* selftest pretend to support all types */ +#endif + if (!support) { rc = -EINVAL; goto out_put_idev; } @@ -250,7 +268,12 @@ int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd) goto out_put_pt; } - klen = iommufd_hwpt_alloc_data_size[cmd->data_type]; + if (cmd->data_type != IOMMU_HWPT_TYPE_SELFTTEST) + klen = iommufd_hwpt_alloc_data_size[cmd->data_type]; +#ifdef CONFIG_IOMMUFD_TEST + else + klen = sizeof(struct iommu_hwpt_selftest); +#endif if (klen) { if (!cmd->data_len) { rc = -EINVAL; diff --git a/drivers/iommu/iommufd/iommufd_test.h b/drivers/iommu/iommufd/iommufd_test.h index 578691602d94..8bbb4f4fa1d7 100644 --- a/drivers/iommu/iommufd/iommufd_test.h +++ b/drivers/iommu/iommufd/iommufd_test.h @@ -115,4 +115,20 @@ struct iommu_hw_info_selftest { __u32 test_reg; }; +/* Should not be equal to any defined value in enum iommu_hwpt_type */ +#define IOMMU_HWPT_TYPE_SELFTTEST 0xbadbeef + +/** + * 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 b50ec3528ec1..b9c7f3bf64b5 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,66 @@ 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 void *user_data) +{ + const struct iommu_hwpt_selftest *user_cfg = user_data; + struct mock_iommu_domain *mock_parent = NULL; + unsigned int iommu_domain_type = IOMMU_DOMAIN_UNMANAGED; + + 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 = @@ -296,6 +338,7 @@ static const struct iommu_ops mock_ops = { .driver_type = IOMMU_HW_INFO_TYPE_SELFTEST, .hw_info = mock_domain_hw_info, .domain_alloc = mock_domain_alloc, + .domain_alloc_user = mock_domain_alloc_user, .capable = mock_domain_capable, .set_platform_dma_ops = mock_domain_set_plaform_dma_ops, .default_domain_ops = @@ -308,6 +351,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, +}; + struct iommu_device mock_iommu_device = { .ops = &mock_ops, }; @@ -324,7 +372,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); } From patchwork Thu Mar 9 08:09:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 66660 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp162963wrd; Thu, 9 Mar 2023 00:12:10 -0800 (PST) X-Google-Smtp-Source: AK7set/WljeIZqbVhBBSwb9/ZZu8A5LO2wAgl0JMlV9zD37tKtpW/AxHg65HjxGTt0qfHs+jPFAm X-Received: by 2002:a05:6a20:7d8b:b0:cc:9b29:f621 with SMTP id v11-20020a056a207d8b00b000cc9b29f621mr27369047pzj.42.1678349530413; Thu, 09 Mar 2023 00:12:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678349530; cv=none; d=google.com; s=arc-20160816; b=ZiB53SOathJSeUKI0SKEJ53xc/e3gc4AELVAtv83eL1DxC7U9CDQfpvDsvxhywPw8h BVoo5djQs5lcBipbFhnu4wHkBgglS3fRLlUQDGbovvNX086CbDSsT0xQs4qxlDtu/u3l RaCFrVvGaLUqd0q43HMpVO38gH+I/wO++O17Cc4YdckI37ua7SHUhGbJ93Y7hNrtiw4d i1GMcPRo9+rb7df+ne3VQ6JrVu8lCAL0FKh36kJGIl37uPO3Z3oSI4q9HyRxuIJ9WF5H BGPNgUnbfTzWbNGDEV7gWR7rGiwaa2qFwTdbxIrA5OtIw17nZtgLF+a0oVgA31G63Kc4 YF4g== 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=VKYc2ck4N+nvLsPkNG+OYBc0ISFwVNeXbF2w/nIJ6QY=; b=y9HD7XsfDVPuWMf92a+URxzggmZ/fgoXrf2Tft+fZhnutAaHq1ulgPiEXNpPZQO+9g N3NCfia8JY+3+okuLTJvgx7EgvD0dzLRZuseF1LkyEZ/jPbehe6gCo/TH5aKTmmYAx4S 7qH0A6HqneekUN6psfcQi4yLB1Gi4bpCwGjsiuKSClHjuwwnukTE+CyZbqLJiQWBLMxK Kv0JfTSFHIr6voHnOOal31DM1Jc8UQYOFBHWjlNRqU92BiXIxMCJJs0sAVzhownCp/Ui kR7hWKcC4/ir90cGZu/PIrzK+BvwKFBTw5avKNiEjp+GMF9zAIoksdqt1Y4w/nMOu2Yu 2aHA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=gooyune0; 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-20020a655a01000000b005038290f44csi16795423pgs.527.2023.03.09.00.11.57; Thu, 09 Mar 2023 00:12:10 -0800 (PST) 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=gooyune0; 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 S230287AbjCIIL1 (ORCPT + 99 others); Thu, 9 Mar 2023 03:11:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57780 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230269AbjCIIKa (ORCPT ); Thu, 9 Mar 2023 03:10:30 -0500 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DAF15DB4AD; Thu, 9 Mar 2023 00:09:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1678349398; x=1709885398; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Hl6KGE/GWJvBWXJmrRoyZQJPdHXffgrjU6Y+0qnffPE=; b=gooyune0CosFRbkbrpLZwAQ2YCxcUdlTVuqpLUJxKYvGw1r97cLc2Iwk sh2IX4xgm/u5R+I3rVs8A8ckR33pLxwfBmv+kgteQ8ZG4f4DDQNug14D6 AuZH7X/1EZrROhDaqQyYTW5tN7XTC0eGnHmByUv0el4ggfGylxPwbY5mK bZl6XnVfOPFUMC3KprLqCcd1lYqfUBfrtFpbmTCAYBTHpiDcUy6+4+dfv NXhBUi/+5Y4/aCRkm1ihhvAd3YksnuJV/KLFEvm7it5DG7btCe/YFDp8x Hvdf4lS5YCaxdQqN89MaBdxZwsQWKS/PuJZPd9Xek+mMvIl3JUHlXf0db Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10643"; a="364023222" X-IronPort-AV: E=Sophos;i="5.98,245,1673942400"; d="scan'208";a="364023222" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Mar 2023 00:09:55 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10643"; a="787471444" X-IronPort-AV: E=Sophos;i="5.98,245,1673942400"; d="scan'208";a="787471444" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga002.fm.intel.com with ESMTP; 09 Mar 2023 00:09:44 -0800 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 Subject: [PATCH 10/12] iommufd/selftest: Add coverage for IOMMU_HWPT_ALLOC with user data Date: Thu, 9 Mar 2023 00:09:08 -0800 Message-Id: <20230309080910.607396-11-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230309080910.607396-1-yi.l.liu@intel.com> References: <20230309080910.607396-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 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?1759877036915409772?= X-GMAIL-MSGID: =?utf-8?q?1759877036915409772?= 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 d2ce2ddbdc40..783afd6ccd2e 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -125,6 +125,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 } @@ -197,6 +198,7 @@ FIXTURE_VARIANT(iommufd_ioas) { unsigned int mock_domains; unsigned int memory_limit; + bool new_hwpt; }; FIXTURE_SETUP(iommufd_ioas) @@ -236,6 +238,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, @@ -263,6 +271,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 */ @@ -632,6 +727,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; @@ -665,12 +762,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 b57e1e60f69d..e0e484e1c775 100644 --- a/tools/testing/selftests/iommu/iommufd_utils.h +++ b/tools/testing/selftests/iommu/iommufd_utils.h @@ -116,6 +116,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, + .data_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_set_ioas(int fd, __u32 access_id, unsigned int ioas_id) From patchwork Thu Mar 9 08:09:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 66664 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp163674wrd; Thu, 9 Mar 2023 00:14:06 -0800 (PST) X-Google-Smtp-Source: AK7set/5VfLWQDPqiIWdvL448ku5n0Z6WUU1EQcUj+vatFEOx86giT3n3B9Exe3powWk+YkVlnYy X-Received: by 2002:a17:90a:fb0e:b0:234:13a3:6e67 with SMTP id it14-20020a17090afb0e00b0023413a36e67mr22013366pjb.12.1678349646015; Thu, 09 Mar 2023 00:14:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678349645; cv=none; d=google.com; s=arc-20160816; b=Cf2El3VJSqAoUK0BcPkSvnOJjLX3bHAvvaKYylw6OjaT50apU/BkeTCzL4KQ59ywzm 9MFO6Gt9kG+SWdfUMp1ZhkZ2J3dhDAgMRIiRIKMAmnLb9PWVXlxCOv4LiG1ID1CualH8 UZZcf/i+Q/AZiKNuR9gdUkY/zydDiqLA9cNodpe8LsRgdK3Uq80u+X9IEEUXwP3whiKb urI8p/d+g+dflO8MaQN9RFtv39W2W78iC2pSZkY2d+vivIjBB03SuvZv+cSq8b8U2Aad 8izArzGc8CyvZ/ceEVEiogBvk/6Pn4wc6RFduN8sQaewfTy6l9M0fLzoYKXPxg14FyKQ cZtg== 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=2W/2rI/sww2jqUVzmsW6/+S6GgiBZMugurDPzF57V0s=; b=C0vzrBQaWT1G10SgCboixveGxdwyFFpcsxKVfDF7lGHZNDy/KojLusr/JrufzsSYZH SSs7Z/AaU/2EFg4oUK7d3ZVENj18NeWhgoIRrYmuR8fYcxBT8X+A7WR5qxyZlfxWjIRN HFNpwTUdfrrOPaW4AvOX7j3mzw8NTsiTPKswyzrlO035EopU9S9q5wUI0XoLiwCNK609 m0x/w94/bpF+HadBZN2hiD0SGr96PV9JQpcs/lYBzoAqspRGZIcLjC1g6VQxG1YCfJ2J gKmgPJ73cIx5CPbYjXY5+/seDE3V+xAv6Q8iSzKQiyiTVtYo6vnM5h2WOgELU9GT3j+G dLSg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=KWIrskr7; 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 c4-20020a630d04000000b005077dda0499si3656988pgl.731.2023.03.09.00.13.53; Thu, 09 Mar 2023 00:14:05 -0800 (PST) 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=KWIrskr7; 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 S230389AbjCIIMn (ORCPT + 99 others); Thu, 9 Mar 2023 03:12:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57980 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229954AbjCIILu (ORCPT ); Thu, 9 Mar 2023 03:11:50 -0500 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2FFE4E20C1; Thu, 9 Mar 2023 00:10:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1678349421; x=1709885421; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=C4LaUzIKNDTM/Z5R5uU/50kK5Lu4e+7iggD/P2dtuk4=; b=KWIrskr7S+vyuX7JOw/IZvjv/HYhp2O8DNMsZrWiDfLvDE5/ftD1sc3A gGWCUOYWcw8TuQePJ+bCFEibbxQ138fLRsYZcx2oM/4kN4ZeAWNX6eWuL hQK+Jo/YN5yipw9o/TLeAf9Qn6/KiTB2iW9DXZVCv8Ypa2VVBHwa8/1rf 3h6+8sL3U6ULBJY6roAZNEhHLvrLlxSmNwMiUcPjcSG7rXTpsdh5zKarT 5XmcxkCNGVYS27pDMPvutoRGP2EB2lSmQ1sAqDM1T7y7eeRfq39FA0v3T JRgexqQ6Sd40XFs88CDjQ4QdHeK7M5Imz3ReBhGsurRr5IBiZqM8d4LnJ A==; X-IronPort-AV: E=McAfee;i="6500,9779,10643"; a="364023258" X-IronPort-AV: E=Sophos;i="5.98,245,1673942400"; d="scan'208";a="364023258" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Mar 2023 00:09:55 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10643"; a="787471446" X-IronPort-AV: E=Sophos;i="5.98,245,1673942400"; d="scan'208";a="787471446" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga002.fm.intel.com with ESMTP; 09 Mar 2023 00:09:45 -0800 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 Subject: [PATCH 11/12] iommufd/selftest: Add IOMMU_TEST_OP_MD_CHECK_IOTLB test op Date: Thu, 9 Mar 2023 00:09:09 -0800 Message-Id: <20230309080910.607396-12-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230309080910.607396-1-yi.l.liu@intel.com> References: <20230309080910.607396-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 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?1759877158420324357?= X-GMAIL-MSGID: =?utf-8?q?1759877158420324357?= 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 8bbb4f4fa1d7..4b60a6df0428 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_SET_IOAS, + IOMMU_TEST_OP_MD_CHECK_IOTLB, }; enum { @@ -95,6 +96,9 @@ struct iommu_test_cmd { struct { __u32 ioas_id; } access_set_ioas; + struct { + __u32 iotlb; + } check_iotlb; }; __u32 last; }; diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c index b9c7f3bf64b5..a0d57f00f759 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -667,6 +667,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; @@ -1077,6 +1096,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->create_access.flags); diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c index 783afd6ccd2e..10f1592dc9e7 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -313,6 +313,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 e0e484e1c775..211798190e5b 100644 --- a/tools/testing/selftests/iommu/iommufd_utils.h +++ b/tools/testing/selftests/iommu/iommufd_utils.h @@ -120,6 +120,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 Mar 9 08:09:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 66674 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp167116wrd; Thu, 9 Mar 2023 00:24:16 -0800 (PST) X-Google-Smtp-Source: AK7set8NN5jv08HzoqKgsXYgO/1Or9gOReuOFUGXjaiiJ1v9RYYENKAurbbAS++BuNmk5Fv4Wyg2 X-Received: by 2002:a17:90a:804a:b0:237:8f8a:6e4c with SMTP id e10-20020a17090a804a00b002378f8a6e4cmr21581094pjw.30.1678350256327; Thu, 09 Mar 2023 00:24:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678350256; cv=none; d=google.com; s=arc-20160816; b=zCWkIAsSzMGKGsPMX7IeaS7r1szoaUIxE+l00bch0OcULTx+tdpI468d9DzOWjow7n ekQh2N/MiI9rTYEAOzhVcohWya27JEY1aj9r3plrYsn+JRVza8IV3bxCva9PA3bWpPjT OLRDFwVL0dBr0PIkdziJFRI5CAwvWAsijmS6aRqIfndMU4QuhxfuF+fLk9XYU+SAgACR V3c5C57CcRU7abbR7SUBlacpbgMSn0Co9DjMm1gPmKjPYVy1LJ9dNtB9GUnZaKO4Sdik AvxDIO8TrfpLhKOs67fp7ILUmE4UwO+Y2Yt4rWbHLNRzdTvF7MCfRzQdJRZurV+gJGYL 9djA== 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=LAtlK3guDIfmMFEesVm3Udi1TRbyHgGY8KKw0VAgzQo=; b=ZXFhUacHwRezk3v48tDdGbU1BLgurppBBvVbWBvI9wFoikg9U68kMX+t2q4PYDnHNJ o7Lz5hsmbg0Mwl9mrWOZI2daOJo9nyBZkZEGgzX1GO2dqWeGV2rxnhv/+j5mbOkNlbi6 BFrP9XYPHEGGPnJ52WtaXq7kfI6PykDYAD/U+qoSD5cMheW8i1L867UffaXkZcK9EYEM on0DkZqO9tCBsKbspPC5REnOQ4DLIeGXy3xqJfB5Mvfu72HtCfqvHzfoMkV3M8xIDxAv Qy7D70op/eZ5nXUf4RSs3hAk9gAr8wyzxTNEG41+9HiIH2OGLO464agqqiyAalD+LgMY yHZg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="K/hsj44b"; 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 n5-20020a17090a670500b00233c4b78195si1825272pjj.4.2023.03.09.00.24.04; Thu, 09 Mar 2023 00:24:16 -0800 (PST) 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="K/hsj44b"; 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 S230167AbjCIIMd (ORCPT + 99 others); Thu, 9 Mar 2023 03:12:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56914 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230298AbjCIILp (ORCPT ); Thu, 9 Mar 2023 03:11:45 -0500 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9A09FE1920; Thu, 9 Mar 2023 00:10:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1678349416; x=1709885416; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=HSs8JiamAMJ1gh9lxGGRXZ1q0y+gsHyMnNUluMbRLa8=; b=K/hsj44bLUfPrQUrZIp4mlpojTGJresxyjijhNXYOKRx8+HzzTx7lD2c fN+E+l/VA6jIzctitsx9Rbopu4dh+23aqr+MGAjeJxQUOasaoSni9Hm7q BU0UokrhzCh27SeC/s7TPD154azcp6bo0YMsbnqHSqlwkrFD8+Ckl+tYE op26ogolIiz87lQliN4FmPzNb65VyIppQACovS/ErKt2bKCa8Cjgjc4Ka zNC3+6JKTYur0iHwyG+Ukhk4/+5Fpm0qZxVNXYTYaepkLGLOWGSe2vhOo XFZTidPPSheX+R39S/ASZ6AI+OP1LkWq+L6J7cvEYMF2z91ll/3EJOaeg w==; X-IronPort-AV: E=McAfee;i="6500,9779,10643"; a="364023248" X-IronPort-AV: E=Sophos;i="5.98,245,1673942400"; d="scan'208";a="364023248" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Mar 2023 00:09:55 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10643"; a="787471448" X-IronPort-AV: E=Sophos;i="5.98,245,1673942400"; d="scan'208";a="787471448" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga002.fm.intel.com with ESMTP; 09 Mar 2023 00:09:47 -0800 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 Subject: [PATCH 12/12] iommufd/selftest: Add coverage for IOMMU_HWPT_INVALIDATE ioctl Date: Thu, 9 Mar 2023 00:09:10 -0800 Message-Id: <20230309080910.607396-13-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230309080910.607396-1-yi.l.liu@intel.com> References: <20230309080910.607396-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 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?1759877798670463801?= X-GMAIL-MSGID: =?utf-8?q?1759877798670463801?= 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/hw_pagetable.c | 10 +++++++-- drivers/iommu/iommufd/iommufd_test.h | 10 +++++++++ drivers/iommu/iommufd/selftest.c | 15 +++++++++++++ tools/testing/selftests/iommu/iommufd.c | 8 +++++++ tools/testing/selftests/iommu/iommufd_utils.h | 21 +++++++++++++++++++ 5 files changed, 62 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index 0871da848447..15dd1743d888 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -339,7 +339,8 @@ int iommufd_hwpt_invalidate(struct iommufd_ucmd *ucmd) * data_len should not exceed the size of iommufd_invalidate_buffer. */ if (cmd->data_type == IOMMU_HWPT_TYPE_DEFAULT || !cmd->data_len || - cmd->data_type >= ARRAY_SIZE(iommufd_hwpt_invalidate_info_size)) + (cmd->data_type != IOMMU_HWPT_TYPE_SELFTTEST && + cmd->data_type >= ARRAY_SIZE(iommufd_hwpt_invalidate_info_size))) return -EOPNOTSUPP; hwpt = iommufd_get_hwpt(ucmd, cmd->hwpt_id); @@ -352,7 +353,12 @@ int iommufd_hwpt_invalidate(struct iommufd_ucmd *ucmd) goto out_put_hwpt; } - klen = iommufd_hwpt_invalidate_info_size[cmd->data_type]; + if (cmd->data_type != IOMMU_HWPT_TYPE_SELFTTEST) + klen = iommufd_hwpt_invalidate_info_size[cmd->data_type]; +#ifdef CONFIG_IOMMUFD_TEST + else + klen = sizeof(struct iommu_hwpt_invalidate_selftest); +#endif if (!klen) { rc = -EINVAL; goto out_put_hwpt; diff --git a/drivers/iommu/iommufd/iommufd_test.h b/drivers/iommu/iommufd/iommufd_test.h index 4b60a6df0428..090e9d6abd87 100644 --- a/drivers/iommu/iommufd/iommufd_test.h +++ b/drivers/iommu/iommufd/iommufd_test.h @@ -135,4 +135,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 a0d57f00f759..fe1caba9e2c5 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -351,9 +351,24 @@ static const struct iommu_ops mock_ops = { }, }; +static void 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; + + if (inv_info->flags & IOMMU_TEST_INVALIDATE_ALL) + mock->iotlb = 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, }; struct iommu_device mock_iommu_device = { diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c index 10f1592dc9e7..89a44479835d 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -126,6 +126,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 } @@ -326,6 +327,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 211798190e5b..b1592b4f2049 100644 --- a/tools/testing/selftests/iommu/iommufd_utils.h +++ b/tools/testing/selftests/iommu/iommufd_utils.h @@ -167,6 +167,27 @@ 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_type = IOMMU_HWPT_TYPE_SELFTTEST, + .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_set_ioas(int fd, __u32 access_id, unsigned int ioas_id) {