From patchwork Mon Jul 24 11:03:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 124861 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp1733905vqg; Mon, 24 Jul 2023 04:32:10 -0700 (PDT) X-Google-Smtp-Source: APBJJlG2bMh6gBXAUKvWpwbDdx7dipdQsy6JMuEd2kHHtXlY0J3h9eU4qV+He0Z6jrG/HvBPeaPb X-Received: by 2002:a05:6358:6f1d:b0:134:c37f:4b63 with SMTP id r29-20020a0563586f1d00b00134c37f4b63mr7506003rwn.2.1690198329759; Mon, 24 Jul 2023 04:32:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690198329; cv=none; d=google.com; s=arc-20160816; b=n5KTeB63pfXFcTUn2y3v/hVAJl8TxkOREFKtnMHG1z3ud3pQqPbnNf2DRlE1hAStmT 3OOmBAyaX1G5HAPiRcuesIDpg7jJVe1zKtmfCrPG7TL74LUXD+CasDHA4n3ZUEQeAlOI uFLpLgmqYdp5vJYn11pcPr7hk0c2dc0BoDZi7J2wFIO9q+TyOQScSgNAKx5M5YVvMU8E ESREOS1/NNZTEFDBxQBphyw2Ov+XtE06XQhb+eQq2jFbw2Q40DVoru37G/rNBRFhCoyO Fnp2bEo7VdoIRChr3H02kO1Xyu6zTLntTEM9PxO9MRuxZLgLTDt6Or3t9XmsY7NoMVZc yWDw== 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=TdCCqW6CwmU+Z6qtILQdbzolgESIQSGAvbL7V08947c=; fh=25kVo4cotgMzE250R6E3K5ES7E7C2JawiavlW0h5mKU=; b=TB+2msCziju+Vu2n52mE+fsnP4sDp487YhJYSUaqHC1SYZoMjH1wmvRDHXx1GyfD0Q QRZdGHZYl6lQ+mvGsdXXnX3wDvr2+hvou0Pd3+TiKIIrNOLG60TYFSnHhYZ722EFud8P kVjzd6iTV7xijShRfzt04QKD/739kaSx15leeYVxUzTqZnPTx27pRecm/vZluWoAM/4q UxwSUxatDzfPggwNexsVp/XTeaJ08WeKPIqEV9xiKtLvIukq5Q64BjbOnxjjGwtdI74n U95/U4EAltoRlrwMkn0H00cLn626P6QzFRXlTZUlptU9CQ/t4e1iYHHjbpuFcxcsg+zh QHxw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=QnDquC3p; 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 i4-20020a633c44000000b00563ab5598f1si2518666pgn.307.2023.07.24.04.31.56; Mon, 24 Jul 2023 04:32:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=QnDquC3p; 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 S233369AbjGXLEQ (ORCPT + 99 others); Mon, 24 Jul 2023 07:04:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57840 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233355AbjGXLEM (ORCPT ); Mon, 24 Jul 2023 07:04:12 -0400 Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 20D4CFF; Mon, 24 Jul 2023 04:04:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690196651; x=1721732651; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=3GrlJi/a+lAeB3GC1MTYxABhNsVM8smyv4iDkQdAdoU=; b=QnDquC3pUlhL8g8YXlCBufINSAsksrjBsibDgaJK0gEW8/4QQupcxHxN SlAIlQauqa5mwGiuZOfEstq9iKv06U48CstVlhZf66ACUzNrUJ7VGNgTh BqleQjxklew/7OIb1cj/uxjX3tlYcBEwvjLwYu3DrFBA1QecBuYRLY9Cf Q0gRii8nISkcJTR1cwaTZ7iS79fzrnakDysvXNNKt56hf+/d726NMHh3W qMKXIa8rIKdSrU6BXgkfbG6dzc4RzXo6k9UhbgmifM498U2ZmNVpo4z/I B3x1ONWF8vJAHSsxO5iXsl8iI+Tk0zgyGJtAeEmGTOATtvY1g3k/BNrRe g==; X-IronPort-AV: E=McAfee;i="6600,9927,10780"; a="366301779" X-IronPort-AV: E=Sophos;i="6.01,228,1684825200"; d="scan'208";a="366301779" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jul 2023 04:04:10 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10780"; a="815775745" X-IronPort-AV: E=Sophos;i="6.01,228,1684825200"; d="scan'208";a="815775745" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by FMSMGA003.fm.intel.com with ESMTP; 24 Jul 2023 04:04:09 -0700 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com Subject: [PATCH v3 01/17] iommu: Add new iommu op to create domains owned by userspace Date: Mon, 24 Jul 2023 04:03:50 -0700 Message-Id: <20230724110406.107212-2-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230724110406.107212-1-yi.l.liu@intel.com> References: <20230724110406.107212-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, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772301403668366723 X-GMAIL-MSGID: 1772301403668366723 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(). 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. Suggested-by: Jason Gunthorpe Signed-off-by: Lu Baolu Co-developed-by: Nicolin Chen Signed-off-by: Nicolin Chen Signed-off-by: Yi Liu Reviewed-by: Kevin Tian --- include/linux/iommu.h | 20 ++++++++++++++++++++ include/uapi/linux/iommufd.h | 8 ++++++++ 2 files changed, 28 insertions(+) diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 4199e13b34e6..ecbec2627b63 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -226,6 +226,15 @@ struct iommu_iotlb_gather { bool queued; }; +/* + * The user data to allocate a specific type user iommu domain + * + * This includes the corresponding driver data structures in + * include/uapi/linux/iommufd.h. + */ +union iommu_domain_user_data { +}; + /** * struct iommu_ops - iommu ops and capabilities * @capable: check capability @@ -235,6 +244,13 @@ 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 a user iommu domain corresponding to the input + * @hwpt_type that is defined as enum iommu_hwpt_type in the + * include/uapi/linux/iommufd.h. A returning domain will be + * set to an IOMMU_DOMAIN_NESTED type, upon valid @user_data + * and @parent that is a kernel-managed domain. Otherwise, + * it will be set to an IOMMU_DOMAIN_UNMANAGED type. Return + * ERR_PTR on allocation failure. * @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 @@ -275,6 +291,10 @@ struct iommu_ops { /* Domain allocation and freeing by the iommu driver */ struct iommu_domain *(*domain_alloc)(unsigned iommu_domain_type); + struct iommu_domain *(*domain_alloc_user)(struct device *dev, + enum iommu_hwpt_type hwpt_type, + struct iommu_domain *parent, + const union iommu_domain_user_data *user_data); struct iommu_device *(*probe_device)(struct device *dev); void (*release_device)(struct device *dev); diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index 4295362e7b44..d38bc54fd5f2 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) From patchwork Mon Jul 24 11:03:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 124859 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp1733752vqg; Mon, 24 Jul 2023 04:31:52 -0700 (PDT) X-Google-Smtp-Source: APBJJlHdraMECvy03MGUzVlaSjfVAGbJ97HliGrWBSfhYt3vwA+9ecjkQAZyoTxncHo3IVBvnQqw X-Received: by 2002:a17:903:4285:b0:1b2:a63:9587 with SMTP id ju5-20020a170903428500b001b20a639587mr6736821plb.36.1690198311882; Mon, 24 Jul 2023 04:31:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690198311; cv=none; d=google.com; s=arc-20160816; b=g/8wrXjusGGvzixbU4auRNvQF53pZvjQW104fFv+ffmz98rHbuJS1ZshY8Wp+/qEO0 J+p9px7gxKDHAizOfv71D0hDOUA4TOp/KExBPDTz9cViY6NQ0LfkOYgwODx2tge5lIWI nW+Jj7WdETWJq/3UBXlaEpGVJB4yy7FTOtJZY0DBCwLh5jD/7UIWNa6m0/Qeij24xw4x HrXr6ISlRCaIbUvXIo2OVU/748cunhsHZQpxwSY6G5km71eZS6hI1jIDtV5Yw3BuWl66 79TFpIHqM2YPBqnVofCTTSWjfYFXey00DpMFbLKXI3/Y29TLU1Z2CBxhs6ifFxAA/x4U FmhA== 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=aff2xy3sVPs+NhyHOQXFvsI1+uqNT01UNc507x+TtgM=; fh=25kVo4cotgMzE250R6E3K5ES7E7C2JawiavlW0h5mKU=; b=JMbHbIw4BW35aKR6hR/Nkhl65Lnw6dgDAIoMCGFp8ybZbMuBPlh8DXohrallC8xjhL m9HrByiXqc5h8SQxCue1JN16L8vAmotSGlwXQYLcIZ0vkh3jbnjxZDT6yV5A7oO725Ge /2wf/Xc3TeFNLcPt5VQnaFqNccHjcPTFm0o2mIU6lCcxl6xDvSEVhqKQje19jlYxVXe1 f9/SGzRkLsl+SSkzFXDvYOB2Fe2wy3yxR9X3m3/r0ZVpkssYKyNKRd/zXy+6yY3FHfhg Y0v4OMBeKUYAZE08EYWz5yT+a8j+JzMGEabZIQKzEIME3NVNoqmOAphdqEgXG1j+2dpt yH4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=EeBEUj8+; 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 ko16-20020a17090307d000b001b8698149f2si8150910plb.588.2023.07.24.04.31.38; Mon, 24 Jul 2023 04:31:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=EeBEUj8+; 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 S232948AbjGXLFT (ORCPT + 99 others); Mon, 24 Jul 2023 07:05:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58834 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232640AbjGXLFG (ORCPT ); Mon, 24 Jul 2023 07:05:06 -0400 Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5EBC519B2; Mon, 24 Jul 2023 04:04:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690196680; x=1721732680; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=TwNnGQoy/O9RF24KvlNH55AuSnmg4Ro5e8QjmX39PRQ=; b=EeBEUj8+PA7hW/BSPLA42M0WYkEeRdyVnol+cm0d/vrbg2HQtzSnn4Sl bnu1R2uovtohva4Hd307vKYvJ0Mo1fcZvrKRbzApS+Uc7gm5J7jkAX71C hbvig/xL36t1LhNQhjalmVkMBwO6teFENtEJjVyp4vS4Psb+rfN+w625y 7ijhovuiySnElBp7TyBGTKBqdpqor6+qDA3Gta8TaVeB7ve6q8Z32rzBz Xp+zpUVnR9TRWAv6e0q/MwvLdQ57zJ/r/kgO7ZLl314DuYkbWOhUNMGaM 3iU3P2BydKZExkY2q37jWNB2y5k7Lyq9ed6UGE94nCLqgfxF5xE7Hv4oD A==; X-IronPort-AV: E=McAfee;i="6600,9927,10780"; a="366301786" X-IronPort-AV: E=Sophos;i="6.01,228,1684825200"; d="scan'208";a="366301786" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jul 2023 04:04:12 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10780"; a="815775757" X-IronPort-AV: E=Sophos;i="6.01,228,1684825200"; d="scan'208";a="815775757" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by FMSMGA003.fm.intel.com with ESMTP; 24 Jul 2023 04:04:11 -0700 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com Subject: [PATCH v3 02/17] iommu: Add nested domain support Date: Mon, 24 Jul 2023 04:03:51 -0700 Message-Id: <20230724110406.107212-3-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230724110406.107212-1-yi.l.liu@intel.com> References: <20230724110406.107212-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, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772301384677705469 X-GMAIL-MSGID: 1772301384677705469 From: Lu Baolu Introduce a new domain type for a user I/O page table, which is nested on top of another user space address represented by a UNMANAGED domain. The mappings of a nested domain are managed by user space software, therefore it's unnecessary to have map/unmap callbacks. But the updates of the PTEs in the nested domain page table must be propagated to the caches on both IOMMU (IOTLB) and devices (DevTLB). The nested domain is allocated by the domain_alloc_user op, and attached to the device through the existing iommu_attach_device/group() interfaces. A new domain op, named cache_invalidate_user is added for the userspace to flush the hardware caches for a nested domain through iommufd. No wrapper for it, as it's only supposed to be used by iommufd. Signed-off-by: Lu Baolu Co-developed-by: Nicolin Chen Signed-off-by: Nicolin Chen Signed-off-by: Yi Liu Reviewed-by: Kevin Tian --- include/linux/iommu.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/include/linux/iommu.h b/include/linux/iommu.h index ecbec2627b63..b8f09330b64e 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -66,6 +66,9 @@ struct iommu_domain_geometry { #define __IOMMU_DOMAIN_SVA (1U << 4) /* Shared process address space */ +#define __IOMMU_DOMAIN_NESTED (1U << 5) /* User-managed address space nested + on a stage-2 translation */ + #define IOMMU_DOMAIN_ALLOC_FLAGS ~__IOMMU_DOMAIN_DMA_FQ /* * 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; @@ -350,6 +354,10 @@ struct iommu_ops { * @iotlb_sync_map: Sync mappings created recently using @map to the hardware * @iotlb_sync: Flush all queued ranges from the hardware TLBs and empty flush * queue + * @cache_invalidate_user: Flush hardware TLBs caching user space IO mappings + * @cache_invalidate_user_data_len: Defined length of input user data for the + * cache_invalidate_user op, being sizeof the + * structure in include/uapi/linux/iommufd.h * @iova_to_phys: translate iova to physical address * @enforce_cache_coherency: Prevent any kind of DMA from bypassing IOMMU_CACHE, * including no-snoop TLPs on PCIe or other platform @@ -379,6 +387,9 @@ struct iommu_domain_ops { size_t size); void (*iotlb_sync)(struct iommu_domain *domain, struct iommu_iotlb_gather *iotlb_gather); + int (*cache_invalidate_user)(struct iommu_domain *domain, + void *user_data); + const size_t cache_invalidate_user_data_len; phys_addr_t (*iova_to_phys)(struct iommu_domain *domain, dma_addr_t iova); From patchwork Mon Jul 24 11:03:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 124858 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp1733180vqg; Mon, 24 Jul 2023 04:31:02 -0700 (PDT) X-Google-Smtp-Source: APBJJlFafzPx0XfI7yuwiWxaNXYgJ2tQmI1lyTKF4bquDDd19ODSfUoHZXwvDS8HcmsKP6uLxPfE X-Received: by 2002:a05:6a20:9385:b0:133:2a73:fdd5 with SMTP id x5-20020a056a20938500b001332a73fdd5mr9195762pzh.32.1690198262171; Mon, 24 Jul 2023 04:31:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690198262; cv=none; d=google.com; s=arc-20160816; b=GAJd8W87JVskiQisU6MJVKWD0saUcNT5zQ/yKuDRfhVt3i2GEyOmjX5dI0Xcu0HWcC poSq3M0DBau+HUVXTIFxcFTuJzABZNV1sNtq6sxkV5BZu9VFAJWo+SH1kYXuhydHJXqC aGK0Erucu5UV3GfMhULr9TcuAByd/EeK0Nxw6Q3NBVPDRdaBNNbrwfM+giCQHVgeC8zT /rMN7MTQfHEdy0/mwuHi5iKBy9jNoFk9rkO+WnZgGvrIOXMBCse5GncIwKNl9JXOpeoo VUF1PlPutbh4y00m3pFpdeyDrLNaZu5bvYgeHLEumgzU0E0jakHghD4+TmVM/OaNiKJ0 ulXg== 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=Y5iG6UlEwYKPqU8DS9oP63Em8/V8HE8LmX8l7dPs/O8=; fh=25kVo4cotgMzE250R6E3K5ES7E7C2JawiavlW0h5mKU=; b=XKUML9czXYHunTX5Oon+TfD49mNFxAmQ9xNPa2cAMt2dKFgqsQnmvEChhtyIjsmByZ AQU/lmbR2UGfkSPJhri6y41HQp7sFeEIwQzsYZPhVL/aHzxdj2wS6AJ25Q0nLRP/v70t QEbLztZzUY8gPqh9tf9DujiQfRKxX+mlRbv9tOien+sT0yo3XzO4ZuI5A3gDrNKpmK9s qcVy1BoZAnSZcnc/RysyLwP5r44bQvYes221QZNzo8vzHV1LTL9SHZEMKO8SflDcrKaE XwfPvXCdVwlzh5oiXZvbInJgSFGoqS9P9X9d2JjmgEUdfc2EIJ9imgADAzI6r1cbnh/+ sWZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=B47ozanK; 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 k10-20020a170902c40a00b001bbb56b34e4si40513plk.331.2023.07.24.04.30.48; Mon, 24 Jul 2023 04:31:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=B47ozanK; 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 S233370AbjGXLE0 (ORCPT + 99 others); Mon, 24 Jul 2023 07:04:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57896 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231432AbjGXLEP (ORCPT ); Mon, 24 Jul 2023 07:04:15 -0400 Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9FFBCFF; Mon, 24 Jul 2023 04:04:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690196653; x=1721732653; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=aAXUsuyKGGbCpXCaUQCKZMtfjXOAaLwSJ/Ehi0NXy/Q=; b=B47ozanK+M5x6JoypzENwk7fB0PqaMF71Je77AZnu9m2BkO3F9bbUPFZ ZvXNrF20Jpq8i4uwnZrMB1g+7xxIdxOGe8cwtiume7p6I1D6Or669Rwxy AjQL2UhjyVEh9u+K+NPpxFYCXfA+UPRl3vf39VjHYh213LZOltugBIr7/ 5i7Bthafj72xkEXdadErDRWfncRoMtptkJPH/eW10m+rGObn5Tbc3WZJj fESUy0/6wJQfor5pdpFfFAmuWBLgRXLVf0bu7oxSsLrTZbCrs77gN+O7q 216txGzV/0xx9Ic4via5ZDIYqHJdpN/3MxgqbEetrVPLBMyM0HsmD4b0F g==; X-IronPort-AV: E=McAfee;i="6600,9927,10780"; a="366301795" X-IronPort-AV: E=Sophos;i="6.01,228,1684825200"; d="scan'208";a="366301795" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jul 2023 04:04:13 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10780"; a="815775772" X-IronPort-AV: E=Sophos;i="6.01,228,1684825200"; d="scan'208";a="815775772" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by FMSMGA003.fm.intel.com with ESMTP; 24 Jul 2023 04:04:12 -0700 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com Subject: [PATCH v3 03/17] iommufd/hw_pagetable: Use domain_alloc_user op for domain allocation Date: Mon, 24 Jul 2023 04:03:52 -0700 Message-Id: <20230724110406.107212-4-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230724110406.107212-1-yi.l.liu@intel.com> References: <20230724110406.107212-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, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772301332582192765 X-GMAIL-MSGID: 1772301332582192765 This makes IOMMUFD to use iommu_domain_alloc_user() for iommu_domain creation as IOMMUFD needs to support iommu_domain allocation with parameters from userspace in nesting support. If the iommu driver doesn't provide domain_alloc_user callback then it falls back to use iommu_domain_alloc(). Suggested-by: Jason Gunthorpe Reviewed-by: Lu Baolu Co-developed-by: Nicolin Chen Signed-off-by: Nicolin Chen Signed-off-by: Yi Liu Reviewed-by: Kevin Tian --- drivers/iommu/iommufd/hw_pagetable.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index cf2c1504e20d..1371e96653b2 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -5,6 +5,7 @@ #include #include +#include "../iommu-priv.h" #include "iommufd_private.h" void iommufd_hw_pagetable_destroy(struct iommufd_object *obj) @@ -74,6 +75,7 @@ struct iommufd_hw_pagetable * iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, struct iommufd_device *idev, bool immediate_attach) { + const struct iommu_ops *ops = dev_iommu_ops(idev->dev); struct iommufd_hw_pagetable *hwpt; int rc; @@ -88,10 +90,21 @@ 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 (!hwpt->domain) { - rc = -ENOMEM; - goto out_abort; + if (ops->domain_alloc_user) { + hwpt->domain = ops->domain_alloc_user(idev->dev, + IOMMU_HWPT_TYPE_DEFAULT, + NULL, NULL); + if (IS_ERR(hwpt->domain)) { + rc = PTR_ERR(hwpt->domain); + hwpt->domain = NULL; + goto out_abort; + } + } else { + hwpt->domain = iommu_domain_alloc(idev->dev->bus); + if (!hwpt->domain) { + rc = -ENOMEM; + goto out_abort; + } } /* From patchwork Mon Jul 24 11:03:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 124836 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp1726190vqg; Mon, 24 Jul 2023 04:17:31 -0700 (PDT) X-Google-Smtp-Source: APBJJlHNKWZKujzQ/nrjKy1HAVmtIRv++y+elt/ka55SURuRkFA+2IRv/BD4dNcFiMOMMQIMQWzM X-Received: by 2002:a6b:5c14:0:b0:783:5209:c01 with SMTP id z20-20020a6b5c14000000b0078352090c01mr6004381ioh.17.1690197451246; Mon, 24 Jul 2023 04:17:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690197451; cv=none; d=google.com; s=arc-20160816; b=mNQDkccZEr1TuWHEarffaYYQ+K9CZAM/f5xxaVfboIfMjxI21h/bvzZ1E12tDUcQFz 4sQDrAmUQDT0ID4LUUJV2bTn70N7JfM6TsVDTTZu1LdDuNWrC3otA+gH9LZx3AvgYZMQ 11I1/yCxNjJJnL+ROx0EI801aEe23m5rs0n6HTlDRgBhm0oF+J78cVAnjHE9X3R0aBYF kb1Jlt98Sl0atElLIxq/zF6oESe1Y9rVllNUZ8Igg5dTXFuFqrPx6UwVxNjLtXA+qjJ4 gH/1JqOZKPUomu6yb9FGod08KFDKNlrJ3R9GkJHi8gjPzz2/UlsrdZKsAUNieegG6xvE zGJw== 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=IFAp+KoLo2+MaWnMKf6db0VqxIpZeuBuqsN24v7kWy0=; fh=25kVo4cotgMzE250R6E3K5ES7E7C2JawiavlW0h5mKU=; b=tDEzqAFydMgxlIwyLJEPVojRCNsusGuVLxfm9ng2K5paakJ5aN2RjCBjfWYJTypN+4 rLVmJuQRpEBSD3A4+WSPMZBfXl1wy2QreEdGi8focDF5vCr9/9wnCSUTLdvT5fdhsONd z162P2283xtHu1Binz/Dy/9j6n5hZOGTrrnWKgTnc7yRbg+YVDngz9hJ7wnF1J3wpPUx /W/XpWE2T8l3bm3Pb8yaVT859LMW7ajPfsqyHYMOxr8a9BKfdUvYuGYaULUFgR2xs1jZ tiS2kEFiCiXU+wU5YeS47ZKHFZDeNP2swh/H16HXsXef34Lgc9H+XNxQ1e4vd3ZlEGhC oF2g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="ljz35/5j"; 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 m29-20020a63581d000000b00543a6cc74bfsi8776241pgb.634.2023.07.24.04.17.17; Mon, 24 Jul 2023 04:17:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="ljz35/5j"; 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 S233414AbjGXLEf (ORCPT + 99 others); Mon, 24 Jul 2023 07:04:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58202 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233396AbjGXLE1 (ORCPT ); Mon, 24 Jul 2023 07:04:27 -0400 Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 97786E7C; Mon, 24 Jul 2023 04:04:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690196660; x=1721732660; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=YsgoeQkQQrPWohp2NoLi++Hed46Wb48fWZYPCiqb+vg=; b=ljz35/5jYPMTE1n8yZYp2VOFCeq8utetdag7ZCqOtZU9TowF7qWwzjUU 9gXwR8PFH89+pczQvJCPXfl94ksXGc3+e1PynBD07zSPtX5MVLiTb61p1 uBId9RPs/IF77uE12vXwTq+X+YWOAnfyF6wdmESs32uuOEhTga74KEs35 DdkNO0ams0BvvucjS3PVsMP/cVemhlKKUVZ6WcLv7SLxcCNZKKjFRNASq 6SrWtrgSfHQfZI9LpqA5HwLcolaCmcg9L0St9bqf2QAES8ZjzjmADZTvA 6JajbT9XBjiGfk+TCzTt2sdNYKy3kA0LuX4fn/g/OPGaejOYu1/CLY3+f g==; X-IronPort-AV: E=McAfee;i="6600,9927,10780"; a="366301802" X-IronPort-AV: E=Sophos;i="6.01,228,1684825200"; d="scan'208";a="366301802" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jul 2023 04:04:20 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10780"; a="815775782" X-IronPort-AV: E=Sophos;i="6.01,228,1684825200"; d="scan'208";a="815775782" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by FMSMGA003.fm.intel.com with ESMTP; 24 Jul 2023 04:04:13 -0700 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com Subject: [PATCH v3 04/17] iommufd: Pass in hwpt_type/parent/user_data to iommufd_hw_pagetable_alloc() Date: Mon, 24 Jul 2023 04:03:53 -0700 Message-Id: <20230724110406.107212-5-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230724110406.107212-1-yi.l.liu@intel.com> References: <20230724110406.107212-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, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772300482581722220 X-GMAIL-MSGID: 1772300482581722220 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 Reviewed-by: Kevin Tian --- drivers/iommu/iommufd/device.c | 3 +- drivers/iommu/iommufd/hw_pagetable.c | 40 +++++++++++++++++++++---- drivers/iommu/iommufd/iommufd_private.h | 8 ++++- 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 0deb2a2ec01a..97e4e5f5aca0 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -542,7 +542,8 @@ iommufd_device_auto_get_domain(struct iommufd_device *idev, } hwpt = iommufd_hw_pagetable_alloc(idev->ictx, ioas, idev, - immediate_attach); + IOMMU_HWPT_TYPE_DEFAULT, + 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 1371e96653b2..28122a49b529 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -24,6 +24,8 @@ void iommufd_hw_pagetable_destroy(struct iommufd_object *obj) if (hwpt->domain) iommu_domain_free(hwpt->domain); + if (hwpt->parent) + refcount_dec(&hwpt->parent->obj.users); refcount_dec(&hwpt->ioas->obj.users); } @@ -61,6 +63,9 @@ 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 + * @hwpt_type: Requested type of hw_pagetable + * @parent: Optional parent HWPT to associate with + * @user_data: Optional user_data pointer * @immediate_attach: True if idev should be attached to the hwpt * * Allocate a new iommu_domain and return it as a hw_pagetable. The HWPT @@ -73,14 +78,24 @@ 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, + enum iommu_hwpt_type hwpt_type, + struct iommufd_hw_pagetable *parent, + union iommu_domain_user_data *user_data, + bool immediate_attach) { const struct iommu_ops *ops = dev_iommu_ops(idev->dev); + struct iommu_domain *parent_domain = NULL; struct iommufd_hw_pagetable *hwpt; int rc; lockdep_assert_held(&ioas->mutex); + if (parent && !user_data) + return ERR_PTR(-EINVAL); + if (user_data && !ops->domain_alloc_user) + return ERR_PTR(-EOPNOTSUPP); + hwpt = iommufd_object_alloc(ictx, hwpt, IOMMUFD_OBJ_HW_PAGETABLE); if (IS_ERR(hwpt)) return hwpt; @@ -89,11 +104,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, - IOMMU_HWPT_TYPE_DEFAULT, - NULL, NULL); + hwpt->domain = ops->domain_alloc_user(idev->dev, hwpt_type, + parent_domain, user_data); if (IS_ERR(hwpt->domain)) { rc = PTR_ERR(hwpt->domain); hwpt->domain = NULL; @@ -107,6 +126,15 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, } } + /* It must be either NESTED or UNMANAGED, depending on parent_domain */ + if (WARN_ON_ONCE((parent_domain && + hwpt->domain->type != IOMMU_DOMAIN_NESTED) || + (!parent_domain && + hwpt->domain->type != IOMMU_DOMAIN_UNMANAGED))) { + rc = -EINVAL; + goto out_abort; + } + /* * Set the coherency mode before we do iopt_table_add_domain() as some * iommus have a per-PTE bit that controls it and need to decide before @@ -168,7 +196,9 @@ 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, + IOMMU_HWPT_TYPE_DEFAULT, + NULL, NULL, false); if (IS_ERR(hwpt)) { rc = PTR_ERR(hwpt); goto out_unlock; diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index dba730129b8c..90dcf4041530 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -8,6 +8,7 @@ #include #include #include +#include struct iommu_domain; struct iommu_group; @@ -243,6 +244,7 @@ int iommufd_vfio_ioas(struct iommufd_ucmd *ucmd); */ struct iommufd_hw_pagetable { struct iommufd_object obj; + struct iommufd_hw_pagetable *parent; struct iommufd_ioas *ioas; struct iommu_domain *domain; bool auto_domain : 1; @@ -254,7 +256,11 @@ 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, + enum iommu_hwpt_type hwpt_type, + struct iommufd_hw_pagetable *parent, + union iommu_domain_user_data *user_data, + bool immediate_attach); int iommufd_hw_pagetable_enforce_cc(struct iommufd_hw_pagetable *hwpt); int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, struct iommufd_device *idev); From patchwork Mon Jul 24 11:03:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 124835 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp1726147vqg; Mon, 24 Jul 2023 04:17:27 -0700 (PDT) X-Google-Smtp-Source: APBJJlGNeLfF4PaYKWTBrra7U1zf2+DHZu2Ha9Hw/ktZhvbnkQpghbwyls/i7eqUVdUMWkDsWjGY X-Received: by 2002:a17:90a:e650:b0:267:f1e1:5a51 with SMTP id ep16-20020a17090ae65000b00267f1e15a51mr3382028pjb.35.1690197447287; Mon, 24 Jul 2023 04:17:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690197447; cv=none; d=google.com; s=arc-20160816; b=yiec+ttq7dXWO3FFMW9/gRyFzawCkrkAm356h9drTwbLAXz/voXQ9OUUn/PM1pXvRt CxzfANP00I629c5O8iodM2654h9OzKcm7S8wZnJYELvYZWdfF1wvkrJg9j3WaUjoNMGv el7gAJS6I3vXd4KAPkUQv+7jaxPrQ2L2eJ2tBqLdJyK73JCLH6dnLOsNJRRZ5PKTX1FY acPIwZfNYYj6MafrGjf2EwFsrMK0F8lEL5NGObxeT+TbCgZZ9bWqucyRKfXKxNtiDc08 sWEGd68nZBzOKOYiEgtQAJ7FkXCe8rVjvgUnuitb2ZtWkfR3HirWXA5B3mL/fX3k03Wl go1A== 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=pAY0iXm5oUSja5dcUyz1xLXXrnv9wkB5uArqdJgoy+E=; fh=25kVo4cotgMzE250R6E3K5ES7E7C2JawiavlW0h5mKU=; b=rcx/JvopkoDtDBBWBLuFXKAuSmOEwgEpncDUXba3xI2VobvZR3ppY72SA/iuxD8f6V TQ9QfDZJ1BxukCAeCzs1eKj91zc9sDUo9x6TKam4NUH5u4GWHLrzniasDppFZVmB8Yg6 M3zGcGcXUeDNhNiuQrh+FUtYVycd8u8vHNSdS2rz75MxfTgOMhxvvoU6YiZDmOAmkU4S Rd1lEWRakybj1NkmoDbFUkdCD7X/SPUx1agQmCLTiVMPGYYxd67gN5YZIJteofjhNf7E pZ11eP4N7yTq2H9EoF7MXZ6Useo/W0/EHygty/uxwuCbUaKulknZyHTu7CWY4rBnvu9V lfbA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=eLI1z2pP; 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 n4-20020a17090a5a8400b0025bdfe1bc66si11351751pji.165.2023.07.24.04.17.13; Mon, 24 Jul 2023 04:17:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=eLI1z2pP; 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 S231807AbjGXLEa (ORCPT + 99 others); Mon, 24 Jul 2023 07:04:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58128 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233385AbjGXLEZ (ORCPT ); Mon, 24 Jul 2023 07:04:25 -0400 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CB9A81B3; Mon, 24 Jul 2023 04:04:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690196656; x=1721732656; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=n7hWgi3n+9uv44qqUfgpCeLTeQHcAqVitGGJSoCyeqw=; b=eLI1z2pPDzHmZJjBw5mrnUNjsiKCYoXh9S9f31CeY4PhtRCzKT+yumBL ZyLe3tPuSz3E6Jd31RjOJZ+ZCd3tsNnYEvKr27LXN4CbUrb/CMF2ZLnw2 N3bUOeNj2FLMSaVW4MZt2+oGkNhBfK0Gf9OC+xqE4t2xJjgB0Z2SpbDmN 7l+qTO50WWn/r5KBYJvO+ItnAhkGjaYMh988bQOKVIGgKA9/hyYSySr9P WrrzxBjQxsWj864bbcmpAnNFplEMEyjYjLvHXkPp9iXIXY0506OawQgIL LUH9MPPHNfaIBxQDWzg9/s6A8k440rFmTVSo01TCkOB/wEy3oPvYh/fOi w==; X-IronPort-AV: E=McAfee;i="6600,9927,10780"; a="347013278" X-IronPort-AV: E=Sophos;i="6.01,228,1684825200"; d="scan'208";a="347013278" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jul 2023 04:04:16 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10780"; a="815775792" X-IronPort-AV: E=Sophos;i="6.01,228,1684825200"; d="scan'208";a="815775792" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by FMSMGA003.fm.intel.com with ESMTP; 24 Jul 2023 04:04:15 -0700 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com Subject: [PATCH v3 05/17] iommufd/hw_pagetable: Do not populate user-managed hw_pagetables Date: Mon, 24 Jul 2023 04:03:54 -0700 Message-Id: <20230724110406.107212-6-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230724110406.107212-1-yi.l.liu@intel.com> References: <20230724110406.107212-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, SPF_HELO_PASS,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772300478780404075 X-GMAIL-MSGID: 1772300478780404075 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 Reviewed-by: Kevin Tian --- drivers/iommu/iommufd/hw_pagetable.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index 28122a49b529..c7301cf0e85a 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -58,6 +58,24 @@ int iommufd_hw_pagetable_enforce_cc(struct iommufd_hw_pagetable *hwpt) return 0; } +static int iommufd_hw_pagetable_link_ioas(struct iommufd_hw_pagetable *hwpt) +{ + int rc; + + /* + * Only a parent hwpt needs to be linked to the IOAS. And a hwpt->parent + * must be linked to the IOAS already, when it's being allocated. + */ + if (hwpt->parent) + return 0; + + rc = iopt_table_add_domain(&hwpt->ioas->iopt, hwpt->domain); + if (rc) + return rc; + list_add_tail(&hwpt->hwpt_item, &hwpt->ioas->hwpt_list); + return 0; +} + /** * iommufd_hw_pagetable_alloc() - Get an iommu_domain for a device * @ictx: iommufd context @@ -160,10 +178,9 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, goto out_abort; } - rc = iopt_table_add_domain(&hwpt->ioas->iopt, hwpt->domain); + rc = iommufd_hw_pagetable_link_ioas(hwpt); if (rc) goto out_detach; - list_add_tail(&hwpt->hwpt_item, &hwpt->ioas->hwpt_list); return hwpt; out_detach: From patchwork Mon Jul 24 11:03:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 124844 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp1727396vqg; Mon, 24 Jul 2023 04:19:42 -0700 (PDT) X-Google-Smtp-Source: APBJJlEMHzYwQ6+HmJHFnXvPyX7FumkIUWJAswt0pIfNx8UX1wNZdcxghL9UgF5veZkYmq46cTnr X-Received: by 2002:a17:902:da88:b0:1b8:b433:7fa with SMTP id j8-20020a170902da8800b001b8b43307famr9049605plx.13.1690197582011; Mon, 24 Jul 2023 04:19:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690197581; cv=none; d=google.com; s=arc-20160816; b=yCQGpAbo1FiZu+iJvvhMFIdgMCMMr2qTJSo0R40HAkyRDRRpbBIKCI1rEI5M0J0w2d elG+u9a6WZbW62v7XzhOkMVthC5oqLm7racR2IsvaUsMCAsxAuYBpEGWG/j7Z8z7XNBZ 7XL5mQyVv1CzaxKieIf5JrpIrN79yx+mIfnsyviIZNbtHf6hbLDEJpJ7PT6I3OqxK0j0 k6BYQzAGuQCiBNIaEYv2N65KY7d3Hr+wv4urCwVDbu3cmbj+9MZf+K0XMWWs4ZEy3rAp vyshAHAw1R1TQ9xN+U2WhW4SDLKG/7OR2+sheghQZMs0+JO+ThbcIldBrFgNQG9n+QXq W38w== 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=4OgZwyN4PmzIwo96tgfKQNWyt14CzFLXGcKDYmAYKGA=; fh=25kVo4cotgMzE250R6E3K5ES7E7C2JawiavlW0h5mKU=; b=Iv9ONjX2n1AFwwOK8nY1xbnjBRybGaLJdSnSH9RyaHLKSGfm21CgjjMsxkgHS5pZ1S se6KG9TWVV3v2f+l2LStirXAlOynXmNdYrx2vijWbIStCZ21/OKpusmn9VjD2ZXz5uza LH/tzg3IyZfevH4vR/vLJislmTa2bTaz4qudBzdao0aY4JA7cz31ITmD/0qWUyM305h9 n37EXiLEHBQeSItejKAUQwaYSfBkLtQrL4wZEJusOZ7vN6o5dpeLceMqq9F7GALu1365 KqhkR3t1uEBt+rG66VBr2h47x6TMSh7rvzGz7zDTyDmjvO3b1NDVGav7dMvuQgKrwmzw SIww== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=gj6NQiKc; 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 u17-20020a170903125100b001b89551a392si9760582plh.113.2023.07.24.04.19.28; Mon, 24 Jul 2023 04:19:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=gj6NQiKc; 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 S231758AbjGXLFk (ORCPT + 99 others); Mon, 24 Jul 2023 07:05:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59356 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231179AbjGXLFZ (ORCPT ); Mon, 24 Jul 2023 07:05:25 -0400 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 04E67E5A; Mon, 24 Jul 2023 04:05:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690196703; x=1721732703; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=mg550ofGRCkx0TRyX/k25V5Hgrta9fIM/SqDWuOS38s=; b=gj6NQiKc51nKpC8PxC4Ustoo0aWgDUQU1+U+DIC1Cj0waGACJCGaK4wd ISXGQCQPPOXZNx1iIJakdE+kiJUna9rmd6wkEsmE6gg+yHe+sxdglyK7z p6L/Yjb2t0GLPr+L8W1Ez9SZ0Qx9dTTJzrNEE8QXg7hM6/iGdaSBQK+Kj QSy9Xpy7tjbjrSeJM7rlZN6sGxtTQmz5d+Vjr9I4wf6e+75/9X1codcl6 76qcibaB+Lx2Qf7FZpiGmH7wsPNSRpvhFrpTe5cencMdF/iiQOfhZCpGh JI/jPoZq9cJKD2Xpj1tRUZxXyPp8erCtpDlKl7MmMz9wdEk2eTIuCdVfI A==; X-IronPort-AV: E=McAfee;i="6600,9927,10780"; a="347013287" X-IronPort-AV: E=Sophos;i="6.01,228,1684825200"; d="scan'208";a="347013287" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jul 2023 04:04:17 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10780"; a="815775802" X-IronPort-AV: E=Sophos;i="6.01,228,1684825200"; d="scan'208";a="815775802" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by FMSMGA003.fm.intel.com with ESMTP; 24 Jul 2023 04:04:16 -0700 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com Subject: [PATCH v3 06/17] iommufd: Only enforce IOMMU_RESV_SW_MSI when attaching user-managed HWPT Date: Mon, 24 Jul 2023 04:03:55 -0700 Message-Id: <20230724110406.107212-7-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230724110406.107212-1-yi.l.liu@intel.com> References: <20230724110406.107212-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, SPF_HELO_PASS,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772300619791981916 X-GMAIL-MSGID: 1772300619791981916 From: Nicolin Chen Reserved IOVA regions except for the IOMMU_RESV_DIRECT_RELAXABLE type should be excluded from the device's DMA address space. For single stage translation configuration, such reserved regions are excluded in the attaching HWPT that is managed by kernel. While for nested translation configuration, there are two stage HWPTs, the reserved regions should be excluded in stage-1 HWPT which is managed by userspace. The current code always excludes the reserved regions in the stage-2 HWPT which is kernel managed. This is incorrect. Instead, the reserved regions should be reported to userspace and excluded in stage-1 HWPT. Besides above, the IOMMU_RESV_SW_MSI type region needs to be excluded in the stage-2 HWPT even in the nested translation configuration on ARM. So the IOMMU_RESV_SW_MSI type region should always be excluded in the kernel managed HWPT. This adds a boolean argument to enforce IOMMU_RESV_SW_MSI only, if attaching HWPT is a user-managed one, i.e. hwpt->parent must be valid, resulting "!!hwpt->parent" to be true, hence only add the IOMMU_RESV_SW_MSI region to the stage-2 HWPT reserved iovas. Signed-off-by: Nicolin Chen Signed-off-by: Yi Liu --- drivers/iommu/iommufd/device.c | 6 ++++-- drivers/iommu/iommufd/io_pagetable.c | 5 ++++- drivers/iommu/iommufd/iommufd_private.h | 3 ++- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 97e4e5f5aca0..c0917683097f 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -347,7 +347,8 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, } rc = iopt_table_enforce_dev_resv_regions(&hwpt->ioas->iopt, idev->dev, - &idev->igroup->sw_msi_start); + &idev->igroup->sw_msi_start, + !!hwpt->parent); if (rc) goto err_unlock; @@ -445,7 +446,8 @@ iommufd_device_do_replace(struct iommufd_device *idev, if (hwpt->ioas != old_hwpt->ioas) { list_for_each_entry(cur, &igroup->device_list, group_item) { rc = iopt_table_enforce_dev_resv_regions( - &hwpt->ioas->iopt, cur->dev, NULL); + &hwpt->ioas->iopt, cur->dev, NULL, + !!hwpt->parent); if (rc) goto err_unresv; } diff --git a/drivers/iommu/iommufd/io_pagetable.c b/drivers/iommu/iommufd/io_pagetable.c index 4d095115c2d0..e658261f44ed 100644 --- a/drivers/iommu/iommufd/io_pagetable.c +++ b/drivers/iommu/iommufd/io_pagetable.c @@ -1172,7 +1172,8 @@ void iopt_remove_access(struct io_pagetable *iopt, /* Narrow the valid_iova_itree to include reserved ranges from a device. */ int iopt_table_enforce_dev_resv_regions(struct io_pagetable *iopt, struct device *dev, - phys_addr_t *sw_msi_start) + phys_addr_t *sw_msi_start, + bool sw_msi_only) { struct iommu_resv_region *resv; LIST_HEAD(resv_regions); @@ -1198,6 +1199,8 @@ int iopt_table_enforce_dev_resv_regions(struct io_pagetable *iopt, num_sw_msi++; } + if (sw_msi_only && resv->type != IOMMU_RESV_SW_MSI) + continue; rc = iopt_reserve_iova(iopt, resv->start, resv->length - 1 + resv->start, dev); if (rc) diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 90dcf4041530..268ae0d5ae12 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -79,7 +79,8 @@ void iopt_table_remove_domain(struct io_pagetable *iopt, struct iommu_domain *domain); int iopt_table_enforce_dev_resv_regions(struct io_pagetable *iopt, struct device *dev, - phys_addr_t *sw_msi_start); + phys_addr_t *sw_msi_start, + bool sw_msi_only); int iopt_set_allow_iova(struct io_pagetable *iopt, struct rb_root_cached *allowed_iova); int iopt_reserve_iova(struct io_pagetable *iopt, unsigned long start, From patchwork Mon Jul 24 11:03:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 124842 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp1726945vqg; Mon, 24 Jul 2023 04:18:52 -0700 (PDT) X-Google-Smtp-Source: APBJJlEdcTWhLzEyUg5wRh4HBXaQ4pAYvsUpeL7e5NJDxX84E+2u1hHt5GN65T+ASDixu4vnqI/D X-Received: by 2002:a17:906:59:b0:994:54e9:692c with SMTP id 25-20020a170906005900b0099454e9692cmr10853276ejg.1.1690197532317; Mon, 24 Jul 2023 04:18:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690197532; cv=none; d=google.com; s=arc-20160816; b=ZWJXVMj+7+bIUp/lBKyeUdb4U1Vn65/zB+s8EPkiQ/Ulw03bn4cu4xLPune8R2NvBu fule1KPfnJFnXdMqBK6tytCps7IFs7bANJg/H6MTB6vO4OWhD8L9FCJlZtSTHx/zJ0QA AGNABsGRQeDWLKl0wB7EoOctRVnBFaXp0w4LvW5E8gYkr5iaFVSEAs9mXk1/GD2c9zwc fSIYDTgCj3UWA11HnBzNpPpkz1FmvPFvu97Fx9jXxGhZroFjFlf9g+SOxoLymWMPTET/ ouo7bRCFv0mlbfA7sBQ2fRMgFuLsjynHmEne672U258Pn4ALpCNKbbB3IjWnMVGlg6Fv aqoA== 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=EAtUnJ2DGeePrEi31FJzOXpe7GFYYh3+5ZeWQV4AQ1o=; fh=25kVo4cotgMzE250R6E3K5ES7E7C2JawiavlW0h5mKU=; b=GHj35hJkIggkdW9xskzAzJh69KLk8ugAihBbQrQmbhsEU8VzwwXAp/uJfM+G/oSwWZ p3KECHbpCf2tw9V1jRixegUr8HkePvWiuR6kq9+AsPSkiRXP0G+Xz57+Kuic4B8LJqkb SpD92I4rcATPORo3LzDKk2oiXrpPtPt9Xb16IdF0XYy64YZcAvj9LEWtguJq4ZNfcRAC jtrjMXABJYOMjSRE5+r4vqIr2eM5nyqG31uXn41xQTOVCALRhIdHk1nLmpzFSf7RYj5w W4Idi/Juh+ddokibuCMBUYjkGzGVVQrlRMCJNKoRJlpArg6RYY4hqqi8e+40ywMuRFUG E8KA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ljU6luiu; 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 lw2-20020a170906bcc200b0098759716e36si6337475ejb.217.2023.07.24.04.18.26; Mon, 24 Jul 2023 04:18:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ljU6luiu; 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 S229598AbjGXLEt (ORCPT + 99 others); Mon, 24 Jul 2023 07:04:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58206 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232542AbjGXLEg (ORCPT ); Mon, 24 Jul 2023 07:04:36 -0400 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3927A124; Mon, 24 Jul 2023 04:04:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690196666; x=1721732666; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=9g6bhAIb+d+E1XevtMPLGEYTgSwXzpUWm9YZpFWGFC0=; b=ljU6luiudRpQ0piCeoc1ahYaXmg/bcq2u7rQPKxIdnYS94HSqCdp2Dnv Ree4s1eIe0gDConHzV2YHvtcvwX1O3dbM6pjXPAvjTlADQe1HAojPNbW/ dHBTZN+sTeui/8LJXcydrXGBvOrei1eOZPUylI5r1VgS59QO1DXn1bzlv THwHH224v7SlKAd6NFDJzsmLNItZ8fGOBVw+T0G0cswalTcoYv8KH7bUs 2EDS4jtd70ScWjz+EvEIUD0jGbgGnL+gNAdD6y+U6yBcMrOTb4Bsmp7GU 3nn5MIsh38WcNMOWPMCHbVivQ5aIUnvU8ubrJOnBktanFnNPiEQkrKFv/ A==; X-IronPort-AV: E=McAfee;i="6600,9927,10780"; a="347013296" X-IronPort-AV: E=Sophos;i="6.01,228,1684825200"; d="scan'208";a="347013296" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jul 2023 04:04:25 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10780"; a="815775811" X-IronPort-AV: E=Sophos;i="6.01,228,1684825200"; d="scan'208";a="815775811" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by FMSMGA003.fm.intel.com with ESMTP; 24 Jul 2023 04:04:17 -0700 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com Subject: [PATCH v3 07/17] iommufd: Add IOMMU_RESV_IOVA_RANGES Date: Mon, 24 Jul 2023 04:03:56 -0700 Message-Id: <20230724110406.107212-8-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230724110406.107212-1-yi.l.liu@intel.com> References: <20230724110406.107212-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, SPF_HELO_PASS,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772300567878946142 X-GMAIL-MSGID: 1772300567878946142 This reports device's reserved IOVA regions to userspace. This is needed in the nested translation as userspace owns stage-1 HWPT, and userspace needs to exclude the reserved IOVA regions in the stage-1 HWPT hence exclude them in the device's DMA address space. This can also be used to figure out allowed IOVAs of an IOAS. Signed-off-by: Yi Liu --- drivers/iommu/iommufd/main.c | 54 ++++++++++++++++++++++++++++++++++++ include/uapi/linux/iommufd.h | 46 ++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index bd3efc1d8509..510db114fc61 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -250,6 +250,57 @@ static int iommufd_get_hw_info(struct iommufd_ucmd *ucmd) return rc; } +static int iommufd_resv_iova_ranges(struct iommufd_ucmd *ucmd) +{ + struct iommu_resv_iova_ranges *cmd = ucmd->cmd; + struct iommu_resv_iova_range __user *ranges; + struct iommu_resv_region *resv; + struct iommufd_device *idev; + LIST_HEAD(resv_regions); + u32 max_iovas; + int rc; + + if (cmd->__reserved) + return -EOPNOTSUPP; + + idev = iommufd_get_device(ucmd, cmd->dev_id); + if (IS_ERR(idev)) + return PTR_ERR(idev); + + max_iovas = cmd->num_iovas; + ranges = u64_to_user_ptr(cmd->resv_iovas); + cmd->num_iovas = 0; + + iommu_get_resv_regions(idev->dev, &resv_regions); + + list_for_each_entry(resv, &resv_regions, list) { + if (resv->type == IOMMU_RESV_DIRECT_RELAXABLE) + continue; + if (cmd->num_iovas < max_iovas) { + struct iommu_resv_iova_range elm = { + .start = resv->start, + .last = resv->length - 1 + resv->start, + }; + + if (copy_to_user(&ranges[cmd->num_iovas], &elm, + sizeof(elm))) { + rc = -EFAULT; + goto out_put; + } + } + cmd->num_iovas++; + } + rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd)); + if (rc) + goto out_put; + if (cmd->num_iovas > max_iovas) + rc = -EMSGSIZE; +out_put: + iommu_put_resv_regions(idev->dev, &resv_regions); + iommufd_put_object(&idev->obj); + return rc; +} + static int iommufd_fops_open(struct inode *inode, struct file *filp) { struct iommufd_ctx *ictx; @@ -347,6 +398,7 @@ union ucmd_buffer { struct iommu_ioas_map map; struct iommu_ioas_unmap unmap; struct iommu_option option; + struct iommu_resv_iova_ranges resv_ranges; struct iommu_vfio_ioas vfio_ioas; #ifdef CONFIG_IOMMUFD_TEST struct iommu_test_cmd test; @@ -389,6 +441,8 @@ static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = { length), IOCTL_OP(IOMMU_OPTION, iommufd_option, struct iommu_option, val64), + IOCTL_OP(IOMMU_RESV_IOVA_RANGES, iommufd_resv_iova_ranges, + struct iommu_resv_iova_ranges, resv_iovas), IOCTL_OP(IOMMU_VFIO_IOAS, iommufd_vfio_ioas, struct iommu_vfio_ioas, __reserved), #ifdef CONFIG_IOMMUFD_TEST diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index d38bc54fd5f2..f2026cde2d64 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_GET_HW_INFO, + IOMMUFD_CMD_RESV_IOVA_RANGES, }; /** @@ -422,4 +423,49 @@ struct iommu_hw_info { __u32 __reserved; }; #define IOMMU_GET_HW_INFO _IO(IOMMUFD_TYPE, IOMMUFD_CMD_GET_HW_INFO) + +/** + * struct iommu_resv_iova_range - ioctl(IOMMU_RESV_IOVA_RANGE) + * @start: First IOVA + * @last: Inclusive last IOVA + * + * An interval in IOVA space. + */ +struct iommu_resv_iova_range { + __aligned_u64 start; + __aligned_u64 last; +}; + +/** + * struct iommu_resv_iova_ranges - ioctl(IOMMU_RESV_IOVA_RANGES) + * @size: sizeof(struct iommu_resv_iova_ranges) + * @dev_id: device to read resv iova ranges for + * @num_iovas: Input/Output total number of resv ranges for the device + * @__reserved: Must be 0 + * @resv_iovas: Pointer to the output array of struct iommu_resv_iova_range + * + * Query a device for ranges of reserved IOVAs. num_iovas will be set to the + * total number of iovas and the resv_iovas[] will be filled in as space + * permits. + * + * On input num_iovas is the length of the resv_iovas array. On output it is + * the total number of iovas filled in. The ioctl will return -EMSGSIZE and + * set num_iovas to the required value if num_iovas is too small. In this + * case the caller should allocate a larger output array and re-issue the + * ioctl. + * + * Under nested translation, userspace should query the reserved IOVAs for a + * given device, and report it to the stage-1 I/O page table owner to exclude + * the reserved IOVAs. The reserved IOVAs can also be used to figure out the + * allowed IOVA ranges for the IOAS that the device is attached to. For detail + * see ioctl IOMMU_IOAS_IOVA_RANGES. + */ +struct iommu_resv_iova_ranges { + __u32 size; + __u32 dev_id; + __u32 num_iovas; + __u32 __reserved; + __aligned_u64 resv_iovas; +}; +#define IOMMU_RESV_IOVA_RANGES _IO(IOMMUFD_TYPE, IOMMUFD_CMD_RESV_IOVA_RANGES) #endif From patchwork Mon Jul 24 11:03:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 124857 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp1732902vqg; Mon, 24 Jul 2023 04:30:40 -0700 (PDT) X-Google-Smtp-Source: APBJJlGJ0AiKZcQtk+knVFnH67LCiixzw0qfna8OiGdnMEEOtuC50vG+jD3JMV6zYg9CoA4Z0hRa X-Received: by 2002:a05:6358:4288:b0:123:5664:e493 with SMTP id s8-20020a056358428800b001235664e493mr10665148rwc.27.1690198239709; Mon, 24 Jul 2023 04:30:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690198239; cv=none; d=google.com; s=arc-20160816; b=SLh5sFWIwU2BKMF+H0C1QrhfzksuOdybwuHeYouNa1Ow/6APiB1HYhHKInTOxVGydg LXqcBfBKfTpTqFLyk7Oca/X4HOTzCJ19iYUKGdNYsd3yXuqqC/SPtbIVktLkZ7EdQ4Kj jFJ3cclnaHKLP8+HzSk3emO2yfs/eA0FDN4MYr18OiYlRJUDuS9Ao60DmUNQijbASYUm WK65GruAzBkLzliclp4XaJ1PoKyIawVJ2QG7IfY8bea2+hDXtY0nKT8Gpm4DWkSFkacM +f0lwqHN8z9lRNjlMefZRcUYaMEnLrUKc8XxnsWPxcnI2bPeGqJKj3rWkNWCjEeWh/GC IOlA== 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=qACUCDNGEs8XwDrLOx8usY8jSITFmWX9cfm4HBKBa6s=; fh=25kVo4cotgMzE250R6E3K5ES7E7C2JawiavlW0h5mKU=; b=lZADyc5lzl0p+oNbtPvRsP0BjOSyzOSmnre3ikzXQcsBLyPmPGzx8KzIV9BFgkDZRk nbJYhWZgvGLGKQuvG2oh1ReNFquE7JOSJ5vPkqEWhQ+F9u6aLZvXTtSm/+NJg4gFqzXZ uFvlgisblXkv2B1apGsDJWHk3shEdAbsSV36bR2IBG4P/1h946OY/Sp7w9ZR3V/6LZcv mLWjJotaOW1CV32ctH9mTteyS8mQ8zhFD1PEpkUkhsN64pdOJ3mXHu9OEqbndNdSP57y WqqVLqt9eDByrC1hD+LUE2FhMaajY449NMQxlEI4pCQEyzZ5hnIzps87F6jGekvyHtUm zB8g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=YNHVlYJf; 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 12-20020a63144c000000b0053f24deef0dsi8870708pgu.312.2023.07.24.04.30.24; Mon, 24 Jul 2023 04:30:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=YNHVlYJf; 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 S233381AbjGXLEh (ORCPT + 99 others); Mon, 24 Jul 2023 07:04:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58156 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233399AbjGXLE1 (ORCPT ); Mon, 24 Jul 2023 07:04:27 -0400 Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 04778E7E; Mon, 24 Jul 2023 04:04:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690196661; x=1721732661; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=/IpvShswXssQrRx1844//VYqPmGEKT1k2zGzdVCiz9Q=; b=YNHVlYJf9N3sEalOsmMTPOb3RDHqPxqIqaSioX9ig8sQQFYicroaVNdi 4OPWfKCzNjWZ83LQKBFuGX4nE0tbcxQYbU+xZM/AZb9M3gWyWZ5N/WE4o pL/sTFJX+fzM4HKsydar92xoqUDsjxDE1V9u/T94RnIM7ONwIZI0oym5K lLparR1GE7zm1PTaJgO9TDepXMXs4HPeHlBJqxsN9PGR7wEmBDy5brcGN LREQwIn298VL4dt6hLYUMqbx6UT/q+AOsmEZ/F9Q3Czl9db8vvvEdtmR4 0OivhnOchDP7GwpSfOF41UiScqNs3/o/fdc/WXhr0jejWIKW5tU1Vh+TM Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10780"; a="366301816" X-IronPort-AV: E=Sophos;i="6.01,228,1684825200"; d="scan'208";a="366301816" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jul 2023 04:04:20 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10780"; a="815775815" X-IronPort-AV: E=Sophos;i="6.01,228,1684825200"; d="scan'208";a="815775815" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by FMSMGA003.fm.intel.com with ESMTP; 24 Jul 2023 04:04:18 -0700 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com Subject: [PATCH v3 08/17] iommufd: IOMMU_HWPT_ALLOC allocation with user data Date: Mon, 24 Jul 2023 04:03:57 -0700 Message-Id: <20230724110406.107212-9-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230724110406.107212-1-yi.l.liu@intel.com> References: <20230724110406.107212-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, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772301309130166753 X-GMAIL-MSGID: 1772301309130166753 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 to 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. It can be used to allocate user-managed stage-1 hwpt, which requires a parent hwpt to point to the stage-2 translation. Co-developed-by: Nicolin Chen Signed-off-by: Nicolin Chen Signed-off-by: Yi Liu --- drivers/iommu/iommufd/hw_pagetable.c | 65 ++++++++++++++++++++++++---- drivers/iommu/iommufd/main.c | 2 +- include/uapi/linux/iommufd.h | 20 ++++++++- 3 files changed, 77 insertions(+), 10 deletions(-) diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index c7301cf0e85a..97e4114226de 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -193,29 +193,75 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd) { + struct iommufd_hw_pagetable *hwpt, *parent = NULL; + union iommu_domain_user_data *data = NULL; struct iommu_hwpt_alloc *cmd = ucmd->cmd; - struct iommufd_hw_pagetable *hwpt; + struct iommufd_object *pt_obj; struct iommufd_device *idev; struct iommufd_ioas *ioas; - int rc; + int rc = 0; if (cmd->flags || cmd->__reserved) return -EOPNOTSUPP; + if (!cmd->data_len && cmd->hwpt_type != IOMMU_HWPT_TYPE_DEFAULT) + return -EINVAL; idev = iommufd_get_device(ucmd, cmd->dev_id); if (IS_ERR(idev)) return PTR_ERR(idev); - ioas = iommufd_get_ioas(ucmd->ictx, cmd->pt_id); - if (IS_ERR(ioas)) { - rc = PTR_ERR(ioas); + pt_obj = iommufd_get_object(ucmd->ictx, cmd->pt_id, IOMMUFD_OBJ_ANY); + if (IS_ERR(pt_obj)) { + rc = -EINVAL; goto out_put_idev; } + switch (pt_obj->type) { + case IOMMUFD_OBJ_IOAS: + ioas = container_of(pt_obj, struct iommufd_ioas, obj); + break; + case IOMMUFD_OBJ_HW_PAGETABLE: + /* pt_id points HWPT only when hwpt_type is !IOMMU_HWPT_TYPE_DEFAULT */ + if (cmd->hwpt_type == IOMMU_HWPT_TYPE_DEFAULT) { + rc = -EINVAL; + goto out_put_pt; + } + + parent = container_of(pt_obj, struct iommufd_hw_pagetable, obj); + /* + * Cannot allocate user-managed hwpt linking to auto_created + * hwpt. If the parent hwpt is already a user-managed hwpt, + * don't allocate another user-managed hwpt linking to it. + */ + if (parent->auto_domain || parent->parent) { + rc = -EINVAL; + goto out_put_pt; + } + ioas = parent->ioas; + break; + default: + rc = -EINVAL; + goto out_put_pt; + } + + if (cmd->data_len) { + data = kzalloc(sizeof(*data), GFP_KERNEL); + if (!data) { + rc = -ENOMEM; + goto out_put_pt; + } + + rc = copy_struct_from_user(data, sizeof(*data), + u64_to_user_ptr(cmd->data_uptr), + cmd->data_len); + if (rc) + goto out_free_data; + } + mutex_lock(&ioas->mutex); hwpt = iommufd_hw_pagetable_alloc(ucmd->ictx, ioas, idev, - IOMMU_HWPT_TYPE_DEFAULT, - NULL, NULL, false); + cmd->hwpt_type, + parent, data, false); if (IS_ERR(hwpt)) { rc = PTR_ERR(hwpt); goto out_unlock; @@ -232,7 +278,10 @@ int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd) iommufd_object_abort_and_destroy(ucmd->ictx, &hwpt->obj); out_unlock: mutex_unlock(&ioas->mutex); - iommufd_put_object(&ioas->obj); +out_free_data: + kfree(data); +out_put_pt: + iommufd_put_object(pt_obj); out_put_idev: iommufd_put_object(&idev->obj); return rc; diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index 510db114fc61..5f4420626421 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -426,7 +426,7 @@ static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = { IOCTL_OP(IOMMU_GET_HW_INFO, iommufd_get_hw_info, struct iommu_hw_info, __reserved), IOCTL_OP(IOMMU_HWPT_ALLOC, iommufd_hwpt_alloc, struct iommu_hwpt_alloc, - __reserved), + 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 f2026cde2d64..73bf9af91e99 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -364,12 +364,27 @@ enum iommu_hwpt_type { * @pt_id: The IOAS to connect this HWPT to * @out_hwpt_id: The ID of the new HWPT * @__reserved: Must be 0 + * @hwpt_type: One of enum iommu_hwpt_type + * @data_len: Length of the type specific data + * @data_uptr: User pointer to the type specific data * * Explicitly allocate a hardware page table object. This is the same object * type that is returned by iommufd_device_attach() and represents the * underlying iommu driver's iommu_domain kernel object. * - * A HWPT will be created with the IOVA mappings from the given IOAS. + * A kernel-managed HWPT will be created with the mappings from the given + * IOAS via the @pt_id. The @hwpt_type for this allocation can be set to + * either IOMMU_HWPT_TYPE_DEFAULT or a pre-defined type corresponding to + * an I/O page table type supported by the underlying IOMMU hardware. + * + * A user-managed HWPT will be created from a given parent HWPT via the + * @pt_id, in which the parent HWPT must be allocated previously via the + * same ioctl from a given IOAS (@pt_id). In this case, the @hwpt_type + * must be set to a pre-defined type corresponding to an I/O page table + * type supported by the underlying IOMMU hardware. + * + * If the @hwpt_type is set to IOMMU_HWPT_TYPE_DEFAULT, both the @data_len + * and the @data_uptr will be ignored. Otherwise, both must be given. */ struct iommu_hwpt_alloc { __u32 size; @@ -378,6 +393,9 @@ struct iommu_hwpt_alloc { __u32 pt_id; __u32 out_hwpt_id; __u32 __reserved; + __u32 hwpt_type; + __u32 data_len; + __aligned_u64 data_uptr; }; #define IOMMU_HWPT_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HWPT_ALLOC) From patchwork Mon Jul 24 11:03:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 124838 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp1726583vqg; Mon, 24 Jul 2023 04:18:11 -0700 (PDT) X-Google-Smtp-Source: APBJJlFmmlN0ZQ1ZLNcOgxJrzc9gOOqMAbUO58ELWbdyEb3qEAWrd5XS1Qdx3VwR2gbB3W4cR1S7 X-Received: by 2002:aa7:88cf:0:b0:668:73f5:dce0 with SMTP id k15-20020aa788cf000000b0066873f5dce0mr7367816pff.29.1690197490942; Mon, 24 Jul 2023 04:18:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690197490; cv=none; d=google.com; s=arc-20160816; b=IYOQFsQnXV7xmCHTMpszgPI2wPbriG0oa4qs7taEZqxjJt9SJgQ/qV6ub+0fkK6hsV jCnmIXnBPT09NnC7Dc7itremGyL832C3bXGx8u2LUjb9VHClWVmy+Mv8zl6SUK/JlsnT a9otcWl3xFleenJr4c5nG6SC2euHE7SIJPwQJ0/Sq7UBYWBLF/hp7UdrOGXI4j/GhfvH rG1YUUX2Sdo/Ae3Pr17kuvwX5lg91tZvkMh509bBF9Cw6GoQEwT8RGu3p5O5wnj3Mkvv SRcAVRB72OxPL5joHw3+kYjFh4lYwbuLAEMDMqG0u6TEKjofZD4Yip4+v5j7R1vlIYXL nISA== 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=UtHPHIV3vyrX8AKhtyRk51ENVl6gqa9iXjLRk3IHskQ=; fh=25kVo4cotgMzE250R6E3K5ES7E7C2JawiavlW0h5mKU=; b=lP4q3rB2swPoRiAOzYGI/17ThVP/xBVHI4vHQehR7dxGyrZjkafTpBJlAMpkZFIjZo Nn40C14CNOCeXaAfKRm0fpi9htPw3tPlJ8xgw8DmE+0odilLPdTKXf8BeXjCajwjb6VC /brO10wJs614+bhHEz2WLM0BQg3Qo8sImRlNiLVy3CqCAWo9ObjnMVL6YUpsQDILZ0HO difhXUPMnHAoFcyW36kbsHkdKiOey55yBlKNTbPAI5On+aBmkbXdx+VY1OVwTrFlNWU9 y4ojNq4GmXvCfwmFtJ+VjcJUyZh7sIML4BU1kVWe1yAPjcMNnxcyqp+eS/Iz5gqQkeuj rwMQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=eg7199wL; 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 j64-20020a638043000000b005533f397576si9069710pgd.46.2023.07.24.04.17.57; Mon, 24 Jul 2023 04:18:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=eg7199wL; 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 S232965AbjGXLEz (ORCPT + 99 others); Mon, 24 Jul 2023 07:04:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58164 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232603AbjGXLEm (ORCPT ); Mon, 24 Jul 2023 07:04:42 -0400 Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AF3C71BC; Mon, 24 Jul 2023 04:04:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690196667; x=1721732667; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Zr2WqMszpqUfg47WqGxebx7Jn6gU45UPhRPMA0fNuxI=; b=eg7199wLV5n+g8ZCf1EbH00GWNF3JqO63riOq3Gm/IgTZx9376HI8sZA mCnjaOWm8NGpIrOXBKRoONS0NZmk/cGEPgmiBo+YGDR82yL0iwEKpesTX trO3bqDUzRxH2bxgvaWs6uXejukPw++BGAnsdGuiFYNu1wM5yijjqPoY0 e9X2z5ii3E+iM6Upc5TXwMisG9YWfZckaE5F5kGRUy757WSV4oopgdPj9 5W2qZZkOTD/SQYQOLOpCpd/82+0AT8LgGKIQrBnnyaVSgaIyVsYNrgnVN slHmQJ6+nqBWIGPuKGnMhdhFV56lBTmrsuCuYZEMB+Ezxp4e6wqmIoB8U A==; X-IronPort-AV: E=McAfee;i="6600,9927,10780"; a="366301822" X-IronPort-AV: E=Sophos;i="6.01,228,1684825200"; d="scan'208";a="366301822" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jul 2023 04:04:20 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10780"; a="815775822" X-IronPort-AV: E=Sophos;i="6.01,228,1684825200"; d="scan'208";a="815775822" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by FMSMGA003.fm.intel.com with ESMTP; 24 Jul 2023 04:04:19 -0700 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com Subject: [PATCH v3 09/17] iommufd: Add IOMMU_HWPT_INVALIDATE Date: Mon, 24 Jul 2023 04:03:58 -0700 Message-Id: <20230724110406.107212-10-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230724110406.107212-1-yi.l.liu@intel.com> References: <20230724110406.107212-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, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772300524473029676 X-GMAIL-MSGID: 1772300524473029676 In nested translation, the stage-1 page table is user-managed and used by IOMMU hardware, so update of any present page table entry in the stage-1 page table should be followed with an IOTLB invalidation. This adds IOMMU_HWPT_INVALIDATE for stage-1 IOTLB invalidation. Co-developed-by: Nicolin Chen Signed-off-by: Nicolin Chen Signed-off-by: Yi Liu --- drivers/iommu/iommufd/hw_pagetable.c | 45 +++++++++++++++++++++++++ drivers/iommu/iommufd/iommufd_private.h | 9 +++++ drivers/iommu/iommufd/main.c | 3 ++ include/uapi/linux/iommufd.h | 22 ++++++++++++ 4 files changed, 79 insertions(+) diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index 97e4114226de..9064e6d181b4 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -286,3 +286,48 @@ int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd) iommufd_put_object(&idev->obj); return rc; } + +int iommufd_hwpt_invalidate(struct iommufd_ucmd *ucmd) +{ + struct iommu_hwpt_invalidate *cmd = ucmd->cmd; + struct iommufd_hw_pagetable *hwpt; + u32 user_data_len, klen; + u64 user_ptr; + int rc = 0; + + if (!cmd->data_len || cmd->__reserved) + return -EOPNOTSUPP; + + hwpt = iommufd_get_hwpt(ucmd, cmd->hwpt_id); + if (IS_ERR(hwpt)) + return PTR_ERR(hwpt); + + /* Do not allow any kernel-managed hw_pagetable */ + if (!hwpt->parent) { + rc = -EINVAL; + goto out_put_hwpt; + } + + klen = hwpt->domain->ops->cache_invalidate_user_data_len; + if (!hwpt->domain->ops->cache_invalidate_user || !klen) { + rc = -EOPNOTSUPP; + goto out_put_hwpt; + } + + /* + * Copy the needed fields before reusing the ucmd buffer, this + * avoids memory allocation in this path. + */ + user_ptr = cmd->data_uptr; + user_data_len = cmd->data_len; + + rc = copy_struct_from_user(cmd, klen, + u64_to_user_ptr(user_ptr), user_data_len); + if (rc) + goto out_put_hwpt; + + rc = hwpt->domain->ops->cache_invalidate_user(hwpt->domain, cmd); +out_put_hwpt: + iommufd_put_object(&hwpt->obj); + return rc; +} diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 268ae0d5ae12..047317fa4df0 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -270,6 +270,7 @@ iommufd_hw_pagetable_detach(struct iommufd_device *idev); void iommufd_hw_pagetable_destroy(struct iommufd_object *obj); void iommufd_hw_pagetable_abort(struct iommufd_object *obj); int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd); +int iommufd_hwpt_invalidate(struct iommufd_ucmd *ucmd); static inline void iommufd_hw_pagetable_put(struct iommufd_ctx *ictx, struct iommufd_hw_pagetable *hwpt) @@ -281,6 +282,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 5f4420626421..255e8a3c5b0e 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -391,6 +391,7 @@ union ucmd_buffer { struct iommu_destroy destroy; struct iommu_hw_info info; struct iommu_hwpt_alloc hwpt; + struct iommu_hwpt_invalidate cache; struct iommu_ioas_alloc alloc; struct iommu_ioas_allow_iovas allow_iovas; struct iommu_ioas_copy ioas_copy; @@ -427,6 +428,8 @@ static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = { __reserved), IOCTL_OP(IOMMU_HWPT_ALLOC, iommufd_hwpt_alloc, struct iommu_hwpt_alloc, data_uptr), + 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 73bf9af91e99..034da283cd3a 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -48,6 +48,7 @@ enum { IOMMUFD_CMD_HWPT_ALLOC, IOMMUFD_CMD_GET_HW_INFO, IOMMUFD_CMD_RESV_IOVA_RANGES, + IOMMUFD_CMD_HWPT_INVALIDATE, }; /** @@ -486,4 +487,25 @@ struct iommu_resv_iova_ranges { __aligned_u64 resv_iovas; }; #define IOMMU_RESV_IOVA_RANGES _IO(IOMMUFD_TYPE, IOMMUFD_CMD_RESV_IOVA_RANGES) + +/** + * struct iommu_hwpt_invalidate - ioctl(IOMMU_HWPT_INVALIDATE) + * @size: sizeof(struct iommu_hwpt_invalidate) + * @hwpt_id: HWPT ID of target hardware page table for the invalidation + * @data_len: Length of the type specific data + * @__reserved: Must be 0 + * @data_uptr: User pointer to the type specific data + * + * Invalidate the iommu cache for user-managed page table. Modifications + * on user-managed page table should be followed with this operation to + * sync the IOTLB. The data in @data_uptr differs per the hwpt type. + */ +struct iommu_hwpt_invalidate { + __u32 size; + __u32 hwpt_id; + __u32 data_len; + __u32 __reserved; + __aligned_u64 data_uptr; +}; +#define IOMMU_HWPT_INVALIDATE _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HWPT_INVALIDATE) #endif From patchwork Mon Jul 24 11:03: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: 124837 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp1726535vqg; Mon, 24 Jul 2023 04:18:05 -0700 (PDT) X-Google-Smtp-Source: APBJJlFgD274V8S7VIk8M+ucpskMSTlg6Gv1FlR2ogZusMyp5BeJ+8IGT1DSO9IcDKWf0F7QiYNw X-Received: by 2002:a05:6402:70c:b0:522:2f8c:8953 with SMTP id w12-20020a056402070c00b005222f8c8953mr1584790edx.39.1690197485566; Mon, 24 Jul 2023 04:18:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690197485; cv=none; d=google.com; s=arc-20160816; b=iGy03PIdI4bkViz3S4bZqG3heJEaE1AgR9XSHXawbegb+ExSHlkVx5ET375pzLs+pY 1mMUkAy7JBdUwHktcQY4yl6RAor94jO6htgqxoD/jV681Gae+WF3Oqxlke9pqBRTD0tY MtbZGU1zlvwrYNliE8nMZGGH98hjxY54n1bOqJOGdBD//BEoUZB5W+DK3Dxf+v27YK1i qFKcGrXz0pl9CoUE4plyFtibl1Wo9L82pNcK9ymgrlgslgegajt+efY1pP4KQguc76S5 c8gLCqhZVulWjqd0qPJyzbcm8ss8/3QgwSyxJ4kJw4o1YwnZ1Pd5MXhrcttgpMJR+hV/ eS5w== 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=Tbr/Fws9MYIRI9KY5ITTxex1HNUBjBJfTqtKjKk+dMY=; fh=25kVo4cotgMzE250R6E3K5ES7E7C2JawiavlW0h5mKU=; b=rOwc4iLTCoYTwrIGX2TMP1CrC7WvAcbgcc8RhXi3ldOX5XRqXZW2Uk2P56kmuiGNXm Hi5NEVxXG7AR2eELTZtxfdyAUHIrlgR7Eo8sT4GakZNE0mU7+N1Sjac+N2Kc3SwIo8j1 zFr2NtZyqIqaJ7NqeQqNeZ++31IFVSu639xf4pQ+vBRhSoU0yHS6zIECDWJ+CcVe/ykR u6q9wAbKtS/UfE+vJdWB7KbpO+xcGoFxlDVBHe/7ezrK/fvGHgZ/AajWvR5O3kVdIzBc ZHBzvBT2TNnF2L5IxO+ZGPolJh9WioAxA2TCCWsdtcLKFPrKpyRfi3uab662cdRQ8ygV bbDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ZmvauX4X; 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 z9-20020aa7cf89000000b00521029a8eeesi6831535edx.313.2023.07.24.04.17.41; Mon, 24 Jul 2023 04:18:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ZmvauX4X; 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 S231432AbjGXLEw (ORCPT + 99 others); Mon, 24 Jul 2023 07:04:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58156 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231954AbjGXLEh (ORCPT ); Mon, 24 Jul 2023 07:04:37 -0400 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E5A06139; Mon, 24 Jul 2023 04:04:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690196667; x=1721732667; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=KYIs7M+eJopUTxJV4cpg9FxLziL1nbNIFCuxJQtO2rA=; b=ZmvauX4X9v+lLzgaAoMNKOtuebkV+GXuHuhiWLqUwX1F1iSgKubKKvOI C+0DTDkrlmiRTr6w/TIWbEV/YoBn1IL83MCigL0Fn+V8+hbGMDmdCD+Ih R7kKgXXEKpNNft0MneS+sAEBNCL/kYjvpgJapJTRoc4T7DJRVe6OjYlpw mCd+ZjS7QK+b3MXzx32/1GceO+rQc5gOui1Ba5gy4pN5yUC7k3COifiMs oFCFLA1t5mODEeIQ8h3EguZoDMMypK5ova2QAbYmsJPk2qlleSdHWD4j+ 4948IhjHX1sO2Lr5PmQ/3vNViyfjXodAhqGBRFNtp0IJ3uhdv6oYwqnO3 w==; X-IronPort-AV: E=McAfee;i="6600,9927,10780"; a="347013310" X-IronPort-AV: E=Sophos;i="6.01,228,1684825200"; d="scan'208";a="347013310" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jul 2023 04:04:25 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10780"; a="815775830" X-IronPort-AV: E=Sophos;i="6.01,228,1684825200"; d="scan'208";a="815775830" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by FMSMGA003.fm.intel.com with ESMTP; 24 Jul 2023 04:04:20 -0700 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com Subject: [PATCH v3 10/17] iommufd/selftest: Add a helper to get test device Date: Mon, 24 Jul 2023 04:03:59 -0700 Message-Id: <20230724110406.107212-11-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230724110406.107212-1-yi.l.liu@intel.com> References: <20230724110406.107212-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, SPF_HELO_PASS,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772300518328967594 X-GMAIL-MSGID: 1772300518328967594 There is need to get the selftest device (sobj->type == TYPE_IDEV) in multiple places, so have a helper to get it. Signed-off-by: Yi Liu --- drivers/iommu/iommufd/selftest.c | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c index af7459e211ad..72d0c37e0b5e 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -472,29 +472,39 @@ static int iommufd_test_mock_domain(struct iommufd_ucmd *ucmd, return rc; } -/* Replace the mock domain with a manually allocated hw_pagetable */ -static int iommufd_test_mock_domain_replace(struct iommufd_ucmd *ucmd, - unsigned int device_id, u32 pt_id, - struct iommu_test_cmd *cmd) +static struct selftest_obj *iommufd_test_get_device(struct iommufd_ctx *ictx, + u32 id) { struct iommufd_object *dev_obj; struct selftest_obj *sobj; - int rc; /* * Prefer to use the OBJ_SELFTEST because the destroy_rwsem will ensure * it doesn't race with detach, which is not allowed. */ - dev_obj = - iommufd_get_object(ucmd->ictx, device_id, IOMMUFD_OBJ_SELFTEST); + dev_obj = iommufd_get_object(ictx, id, IOMMUFD_OBJ_SELFTEST); if (IS_ERR(dev_obj)) - return PTR_ERR(dev_obj); + return (struct selftest_obj *)dev_obj; sobj = container_of(dev_obj, struct selftest_obj, obj); if (sobj->type != TYPE_IDEV) { - rc = -EINVAL; - goto out_dev_obj; + iommufd_put_object(dev_obj); + return ERR_PTR(-EINVAL); } + return sobj; +} + +/* Replace the mock domain with a manually allocated hw_pagetable */ +static int iommufd_test_mock_domain_replace(struct iommufd_ucmd *ucmd, + unsigned int device_id, u32 pt_id, + struct iommu_test_cmd *cmd) +{ + struct selftest_obj *sobj; + int rc; + + sobj = iommufd_test_get_device(ucmd->ictx, device_id); + if (IS_ERR(sobj)) + return PTR_ERR(sobj); rc = iommufd_device_replace(sobj->idev.idev, &pt_id); if (rc) @@ -504,7 +514,7 @@ static int iommufd_test_mock_domain_replace(struct iommufd_ucmd *ucmd, rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd)); out_dev_obj: - iommufd_put_object(dev_obj); + iommufd_put_object(&sobj->obj); return rc; } From patchwork Mon Jul 24 11:04: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: 124860 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp1733829vqg; Mon, 24 Jul 2023 04:32:03 -0700 (PDT) X-Google-Smtp-Source: APBJJlFMU4NW6rO3iMsHfZKkv/cnhp5tnzOZ8MAlekOoF/6CRy7yFYHNWV9pdUN3Pe7ZJFkUBluN X-Received: by 2002:a05:6358:998f:b0:135:b4c:a4b4 with SMTP id j15-20020a056358998f00b001350b4ca4b4mr4589549rwb.14.1690198322828; Mon, 24 Jul 2023 04:32:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690198322; cv=none; d=google.com; s=arc-20160816; b=leu4JERvJjKsQCxt+YXu37UGOyKMNjD/sHrgY0e3DRwmnx73N1uKjNFiEpFJJ7hMnN TYddK7soE84oZagTFcExfWFh5Ip46/hs5HXOzYm+PJhP5gT3pxTSMOf7cROQDUU1Nmss 1A9LD9QPghk7/NdBCWuflUu9p9G8Go4iE4gvMrkcvGQx+Z1AhbvaN82ZoGfgiCEcgEsa QfdC9DbaNCgx18duaW8+GNMeR70t6/8AA/2WnvJ4V3q+6boSNfI4QGgmsEBZ49DQn9A4 /9IbnIUNI1EKYTDt0CUHNOlglO4y6kDGv8nPysRujr7vNn+1/w/2UumCh+gKvzaxMgCE Gu2A== 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=0xlqIEzyG7xWqhOun1XYTekoGIpXCSlbVEOQEul1pGg=; fh=25kVo4cotgMzE250R6E3K5ES7E7C2JawiavlW0h5mKU=; b=1IMI+F3S7p6sUBJwfpRMjQc1j9g1UeRKE+7suVLwI8BNUnc86fUhSpeXrRGMQbyhHW ZT8oryzSU16BWecWbe5n/mSCMXBBG0HE78pe9L9mWd+8PkPyHLGzQEwWkzMRVPhymP+4 TheiMhigqyiUc9Et5KbFLlxtRkUXsOKdfapidiV5i/AoaBL/kbd3aoyow9quDHu/I5G3 nk9D5/3mdmuWWczwoRNoEIXGoSoysKgTrsjVy9m33YQ0Jx8sb/zPoJFKjqjSIYhTozQq 2KlA144SrxtfjDRorIimVAJnayddFlPKyUssy5sol8P5ILqfiB4NACA0XZJIT7i7Y7rV oomg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="oKE/woZb"; 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 u71-20020a63854a000000b005635382f87dsi9103181pgd.15.2023.07.24.04.31.50; Mon, 24 Jul 2023 04:32:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="oKE/woZb"; 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 S230005AbjGXLFK (ORCPT + 99 others); Mon, 24 Jul 2023 07:05:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58296 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231393AbjGXLFC (ORCPT ); Mon, 24 Jul 2023 07:05:02 -0400 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A13BEE73; Mon, 24 Jul 2023 04:04:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690196676; x=1721732676; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=sy/10kzSsQE8Iw4y4z6S/RygUlRyD0M+tbsCuWgKxuA=; b=oKE/woZbiK1Pj9aexkkDFZRql6RUf925fo1G8xBIHdqUhwwtIIXaIiME pmf/2M1Z59JAWQbqnxQYoWow/wnajaVPZ22ViV76VH4JCTZItPhNL2q2K p84t+TMdM6P3bZASbRblcAyuZ0/mTSR2rC428pEJYzvxEr83l3LRxp8DA EfkG3mAFlUFM7Nlrmjt5AeHC3bMhReUDZdgqAfC35wozjGQNvPL2vBXTe lppjCWPMbuDm+GVzZX6PX+ZODBinmXiFlEOFCAnnklSVY2D4y3KZLSlyP lrdKd7D0pGISW2eEGaKvWNBE+dNL0rpWj8ghTPXE2D32oa2dkiL0XrQcw Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10780"; a="347013313" X-IronPort-AV: E=Sophos;i="6.01,228,1684825200"; d="scan'208";a="347013313" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jul 2023 04:04:25 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10780"; a="815775861" X-IronPort-AV: E=Sophos;i="6.01,228,1684825200"; d="scan'208";a="815775861" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by FMSMGA003.fm.intel.com with ESMTP; 24 Jul 2023 04:04:21 -0700 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com Subject: [PATCH v3 11/17] iommufd/selftest: Add IOMMU_TEST_OP_DEV_[ADD|DEL]_RESERVED to add/del reserved regions to selftest device Date: Mon, 24 Jul 2023 04:04:00 -0700 Message-Id: <20230724110406.107212-12-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230724110406.107212-1-yi.l.liu@intel.com> References: <20230724110406.107212-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, SPF_HELO_PASS,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772301396417999545 X-GMAIL-MSGID: 1772301396417999545 This makes test tool able to specify reserved regions to the selftest device and hence able to validate the ioctl IOMMU_RESV_IOVA_RANGES. Signed-off-by: Yi Liu --- drivers/iommu/iommufd/iommufd_test.h | 6 ++ drivers/iommu/iommufd/selftest.c | 82 ++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) diff --git a/drivers/iommu/iommufd/iommufd_test.h b/drivers/iommu/iommufd/iommufd_test.h index 3f3644375bf1..b7f4bee2ea1b 100644 --- a/drivers/iommu/iommufd/iommufd_test.h +++ b/drivers/iommu/iommufd/iommufd_test.h @@ -19,6 +19,8 @@ enum { IOMMU_TEST_OP_SET_TEMP_MEMORY_LIMIT, IOMMU_TEST_OP_MOCK_DOMAIN_REPLACE, IOMMU_TEST_OP_ACCESS_REPLACE_IOAS, + IOMMU_TEST_OP_DEV_ADD_RESERVED, + IOMMU_TEST_OP_DEV_DEL_RESERVED, }; enum { @@ -50,6 +52,10 @@ struct iommu_test_cmd { __aligned_u64 start; __aligned_u64 length; } add_reserved; + struct { + __aligned_u64 start; + __aligned_u64 length; + } add_dev_reserved; struct { __u32 out_stdev_id; __u32 out_hwpt_id; diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c index 72d0c37e0b5e..271c6c261eb4 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -93,6 +93,8 @@ enum selftest_obj_type { struct mock_dev { struct device dev; + struct rw_semaphore reserved_rwsem; + struct rb_root_cached reserved_itree; }; struct selftest_obj { @@ -385,6 +387,10 @@ static struct mock_dev *mock_dev_create(void) mutex_init(&mdev->dev.iommu->lock); mdev->dev.iommu->iommu_dev = &mock_iommu_device; + /* Reserve range related fields */ + init_rwsem(&mdev->reserved_rwsem); + mdev->reserved_itree = RB_ROOT_CACHED; + rc = device_add(&mdev->dev); if (rc) goto err_dev_iommu; @@ -407,8 +413,20 @@ static struct mock_dev *mock_dev_create(void) return ERR_PTR(rc); } +static void mock_dev_remove_reserved_iovas(struct mock_dev *mdev) +{ + struct interval_tree_node *node; + + while ((node = interval_tree_iter_first(&mdev->reserved_itree, 0, + ULONG_MAX))) { + interval_tree_remove(node, &mdev->reserved_itree); + kfree(node); + } +} + static void mock_dev_destroy(struct mock_dev *mdev) { + mock_dev_remove_reserved_iovas(mdev); iommu_group_remove_device(&mdev->dev); device_del(&mdev->dev); kfree(mdev->dev.iommu); @@ -536,6 +554,64 @@ static int iommufd_test_add_reserved(struct iommufd_ucmd *ucmd, return rc; } +static int mock_dev_add_reserved_iova(struct mock_dev *mdev, + unsigned long start, unsigned long last) +{ + struct interval_tree_node *reserved; + + if (interval_tree_iter_first(&mdev->reserved_itree, start, last)) + return -EADDRINUSE; + + reserved = kzalloc(sizeof(*reserved), GFP_KERNEL_ACCOUNT); + if (!reserved) + return -ENOMEM; + reserved->start = start; + reserved->last = last; + interval_tree_insert(reserved, &mdev->reserved_itree); + return 0; +} + +/* Add a reserved IOVA region to a device */ +static int iommufd_test_dev_add_reserved(struct iommufd_ucmd *ucmd, + unsigned int device_id, + unsigned long start, size_t length) +{ + struct selftest_obj *sobj; + struct mock_dev *mdev; + int rc; + + sobj = iommufd_test_get_device(ucmd->ictx, device_id); + if (IS_ERR(sobj)) + return PTR_ERR(sobj); + + mdev = sobj->idev.mock_dev; + down_write(&mdev->reserved_rwsem); + rc = mock_dev_add_reserved_iova(sobj->idev.mock_dev, + start, start + length - 1); + up_write(&mdev->reserved_rwsem); + iommufd_put_object(&sobj->obj); + return rc; +} + +/* Remove all reserved IOVA regions of a device */ +static int iommufd_test_dev_del_reserved(struct iommufd_ucmd *ucmd, + unsigned int device_id) +{ + struct selftest_obj *sobj; + struct mock_dev *mdev; + + sobj = iommufd_test_get_device(ucmd->ictx, device_id); + if (IS_ERR(sobj)) + return PTR_ERR(sobj); + + mdev = sobj->idev.mock_dev; + down_write(&mdev->reserved_rwsem); + mock_dev_remove_reserved_iovas(mdev); + up_write(&mdev->reserved_rwsem); + iommufd_put_object(&sobj->obj); + return 0; +} + /* Check that every pfn under each iova matches the pfn under a user VA */ static int iommufd_test_md_check_pa(struct iommufd_ucmd *ucmd, unsigned int mockpt_id, unsigned long iova, @@ -1025,6 +1101,12 @@ int iommufd_test(struct iommufd_ucmd *ucmd) return iommufd_test_add_reserved(ucmd, cmd->id, cmd->add_reserved.start, cmd->add_reserved.length); + case IOMMU_TEST_OP_DEV_ADD_RESERVED: + return iommufd_test_dev_add_reserved(ucmd, cmd->id, + cmd->add_dev_reserved.start, + cmd->add_dev_reserved.length); + case IOMMU_TEST_OP_DEV_DEL_RESERVED: + return iommufd_test_dev_del_reserved(ucmd, cmd->id); case IOMMU_TEST_OP_MOCK_DOMAIN: return iommufd_test_mock_domain(ucmd, cmd); case IOMMU_TEST_OP_MOCK_DOMAIN_REPLACE: From patchwork Mon Jul 24 11:04: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: 124863 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp1734274vqg; Mon, 24 Jul 2023 04:32:49 -0700 (PDT) X-Google-Smtp-Source: APBJJlGndCRuOGVKRyg5X1Q2mgVVMB/byvuF2g72M3iVZlFufpG8DeQRjpxum4lQBelzn9tI77J2 X-Received: by 2002:a17:902:d4cc:b0:1a2:a904:c42e with SMTP id o12-20020a170902d4cc00b001a2a904c42emr12873546plg.24.1690198369012; Mon, 24 Jul 2023 04:32:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690198368; cv=none; d=google.com; s=arc-20160816; b=N5FoMTQg7pOr5rFNfRaShnUoG1kTYSETPgRa3Qya42BvPO8AKXZCqg3NUyfRKtyLHb prhGqwJ871KQdyhe8fKoIiyS7tx/UJ8i3tNm3x6OfqYT+dEdCz6K27y8AaPNNXOEtpkm bWtdcQcBe+udktBi6m9yCXtPWsjb6FH76BoKHS5ENNHTbUt2ylhNyU7jDRK+YzAdVeT2 93ySn331HmPow12GJkCmIdalU6verpWH/aceDWUddjcj63Oskurold8MOPWUddD46bCw DuNiVTOl7/UkSQMjZbdWr0cS9CGZbf6gXfua+vEfbrQDHBT/8MJPOPr0CD+HfVSzv+6u 3kHg== 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=JQX9phtgXzaU/yS4G592d2vAzkuwEjYtBco2LWA2ljM=; fh=25kVo4cotgMzE250R6E3K5ES7E7C2JawiavlW0h5mKU=; b=AbHVWdpuOJ9tnSCOCJY+ksJAtLC1bheoj4nZjeICZRyZHO+8ynN6t23txvXmqC/+0R qLlvcWVFDfvn8uygJ5A01/KSrcrhV1Pr8Eo4I3zbofaBowzp8nAMd6wGPWTh1e1TIBjk ppRLJAgmf0+7K85QGlzed+BwmD7TptSbk8mXcTCrHJ2RQ4LZ6zMk8xO9d3DQGgLfKzDJ 0QgvdWEcKxiZE44X00KQQX+EbyPHxx/1vJQDlXns7xcS3XGGeWFhH2UtPTC6rYSJUI8e z+ObGWYIdRmpUe0EL75F8Q565IVAbrIcf6nyYv2ItxN3JnC41Mp+JgrtI311dCRUyTgU Zwvw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="eAYudO/j"; 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 le13-20020a170902fb0d00b001b84f9263e7si8256217plb.325.2023.07.24.04.32.35; Mon, 24 Jul 2023 04:32:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="eAYudO/j"; 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 S232733AbjGXLFP (ORCPT + 99 others); Mon, 24 Jul 2023 07:05:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58610 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230023AbjGXLFD (ORCPT ); Mon, 24 Jul 2023 07:05:03 -0400 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2D4B610C3; Mon, 24 Jul 2023 04:04:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690196677; x=1721732677; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Jdb8ZB5daKdGFPrTc/NRXCndQLaxOJW2ZTy0hfrrfYc=; b=eAYudO/jVBxCKMJ9eWXTRpjrjtpVWzECmCkofr6AUgxEMLLNQnsMDdAv k4FV73GZ7K1zk5/5qzA+PpePp91RSqzQM380wxA89wwuP+ykyhYOCZkqJ wNEb2QB6tSWSfpLIjdnquMIqSRa2hia8RIgxQlu1rqUA0HbdznO4E/PEV CGakfMMYGMAj1J/A/IW5io+KaBm+7O2Pix7T410+QEBKmLVFWPbzcZwH3 ctCRY/vFODNG+eBReRcXRzjuZkaAhW4Tu0gDzyiYAt49sRwX8hb5yeigO gxgwInsdxVl6l+6IyoMNs1SBNX9rfqrKq1l/higY7TnMR0gMLQ6F3q96h w==; X-IronPort-AV: E=McAfee;i="6600,9927,10780"; a="347013321" X-IronPort-AV: E=Sophos;i="6.01,228,1684825200"; d="scan'208";a="347013321" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jul 2023 04:04:25 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10780"; a="815775871" X-IronPort-AV: E=Sophos;i="6.01,228,1684825200"; d="scan'208";a="815775871" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by FMSMGA003.fm.intel.com with ESMTP; 24 Jul 2023 04:04:22 -0700 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com Subject: [PATCH v3 12/17] iommufd/selftest: Add .get_resv_regions() for mock_dev Date: Mon, 24 Jul 2023 04:04:01 -0700 Message-Id: <20230724110406.107212-13-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230724110406.107212-1-yi.l.liu@intel.com> References: <20230724110406.107212-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, SPF_HELO_PASS,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772301444747871350 X-GMAIL-MSGID: 1772301444747871350 This makes mock_dev to report resv_regions. Signed-off-by: Yi Liu --- drivers/iommu/iommufd/selftest.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c index 271c6c261eb4..e40780db88b5 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -292,6 +292,32 @@ static void mock_domain_set_plaform_dma_ops(struct device *dev) */ } +static void iommufd_test_get_resv_regions(struct device *dev, + struct list_head *head) +{ + struct mock_dev *mdev = container_of(dev, struct mock_dev, dev); + struct interval_tree_node *reserved, *next; + + down_read(&mdev->reserved_rwsem); + for (reserved = interval_tree_iter_first(&mdev->reserved_itree, 0, ULONG_MAX); + reserved; reserved = next) { + struct iommu_resv_region *resv; + + next = interval_tree_iter_next(reserved, 0, ULONG_MAX); + /* Only adds IOMMU_RESV_DIRECT so far */ + resv = iommu_alloc_resv_region(reserved->start, + reserved->last + 1 - reserved->start, + IOMMU_READ | IOMMU_WRITE, + IOMMU_RESV_DIRECT, + GFP_ATOMIC); + if (!resv) + break; + + list_add_tail(&resv->list, head); + } + up_read(&mdev->reserved_rwsem); +} + static const struct iommu_ops mock_ops = { .owner = THIS_MODULE, .pgsize_bitmap = MOCK_IO_PAGE_SIZE, @@ -299,6 +325,7 @@ static const struct iommu_ops mock_ops = { .hw_info = mock_domain_hw_info, .domain_alloc = mock_domain_alloc, .capable = mock_domain_capable, + .get_resv_regions = iommufd_test_get_resv_regions, .set_platform_dma_ops = mock_domain_set_plaform_dma_ops, .default_domain_ops = &(struct iommu_domain_ops){ From patchwork Mon Jul 24 11:04: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: 124841 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp1726936vqg; Mon, 24 Jul 2023 04:18:51 -0700 (PDT) X-Google-Smtp-Source: APBJJlFq5wJvIIp3c8Rkgl6Gng6dJMw2Q/4Gs/MXDNdg62kmnZeTt6dQbSeC3NWJo9dBiofFtB4L X-Received: by 2002:a17:902:a507:b0:1bb:809d:ae6a with SMTP id s7-20020a170902a50700b001bb809dae6amr7342287plq.7.1690197531589; Mon, 24 Jul 2023 04:18:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690197531; cv=none; d=google.com; s=arc-20160816; b=GnGIsAnhDPKAUACfDYHoLQ4KBwZVcmLSZh4AXqfn9SZaGLIuvfR1CxhrRgajIXY/ac o8NS0hDghxl+MjPwpOQInX9JGx7iO23fe683yOQ7zVvqfLrLL0Zp61MtUDVC3OG8mrE1 u5fOb+LUlj5fmfW4XAB7FqIiN3+JDonjsxYUeHTW1WDmSL2EYLVDnXN1tBU1OMUhG9ea 3jYdDrH4Twae9SMPbn0z7flY3EZ4HkrHRLR+Xk+Q59PgN8nQm5oddA/CgCf7+bc4KKYM aGrwmaHLBlp18bPa3vnrdTsYKy4Pe/Kvw+WHYjVDS1EelW4Mm32EnGeJKZ7sN+rDUhh9 0tOg== 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=n54UUvMoEuYwbiJkG9ElAPEITFbOBWDjZYVpVGI8HhE=; fh=25kVo4cotgMzE250R6E3K5ES7E7C2JawiavlW0h5mKU=; b=tjUwjamYxi6MkQVTFfyKZHVVYlioWnlO89plJWrPfd0i/IEy0XtddjWbFizTBsPefW lj3wUDtdwBgJoVCv+ZhXuLm63r1yfdQYJdpSZTwkdZmtA5pN39AEmWEGYcahf7Pm0NlS tW8dZR01I6jhP50N9vxBRT+soc/ZW8QGAhlkogVuHNrko3ykF6fwc5awE0GeZ4iLzRHC sLaiOHg/ZIFcEnd8pwaZP1EH2z/aLngG9a4KN3l4LCtQuY8stYLQfR+OPiiVvMuOktln RpPi/IJ+hkGbic4imTzhGcwpCX8aSZLt75hKMQf+scJnT/H9MPkOIrqGlX+Qtb+aH1wj p2bw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=HBd3gh2M; 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 kw11-20020a170902f90b00b001b8ba3926dbsi8275248plb.568.2023.07.24.04.18.36; Mon, 24 Jul 2023 04:18:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=HBd3gh2M; 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 S231608AbjGXLFX (ORCPT + 99 others); Mon, 24 Jul 2023 07:05:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58172 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231179AbjGXLFK (ORCPT ); Mon, 24 Jul 2023 07:05:10 -0400 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7E96410DE; Mon, 24 Jul 2023 04:04:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690196685; x=1721732685; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=wu92GRhppcU7MkbkHzcmjc/NGunkuHuJrkG4JF9bmNM=; b=HBd3gh2MN1t0DXy55YYzU2w9I1Os1VxvP2JcHFX6qPLfz/2id9h/4lbQ zf5r44/Pp69VqtAVq9i7mW09th+o7ghpuACqs0E6r1LuqOa3qQUqUmhLi 2WJlKwMyZdrRMuLaIzLYiNLTR7rSQmIK04WxsvqPmXh6yPd1i37lUVvwE c+8+8SInIbYSdN68edcvhTtPvg8KMjgcr+2kLghffArAqBRnO4RBScdvk dEtNl0v352YtsWPKm7CA1abnb2A4SsyBAC+XbTt46Wmp5L1I7S3PBoOhp YHCOdI/2rOtAondsq26tBCC7VcPE3MMB7d99PGNI6R2QEse2S59vH2zyQ g==; X-IronPort-AV: E=McAfee;i="6600,9927,10780"; a="347013339" X-IronPort-AV: E=Sophos;i="6.01,228,1684825200"; d="scan'208";a="347013339" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jul 2023 04:04:25 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10780"; a="815775877" X-IronPort-AV: E=Sophos;i="6.01,228,1684825200"; d="scan'208";a="815775877" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by FMSMGA003.fm.intel.com with ESMTP; 24 Jul 2023 04:04:22 -0700 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com Subject: [PATCH v3 13/17] iommufd/selftest: Add coverage for IOMMU_RESV_IOVA_RANGES Date: Mon, 24 Jul 2023 04:04:02 -0700 Message-Id: <20230724110406.107212-14-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230724110406.107212-1-yi.l.liu@intel.com> References: <20230724110406.107212-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, SPF_HELO_PASS,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772300566503512654 X-GMAIL-MSGID: 1772300566503512654 This adds selftest for ioctl IOMMU_RESV_IOVA_RANGES. Signed-off-by: Yi Liu --- tools/testing/selftests/iommu/iommufd.c | 98 +++++++++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c index 6b075a68b928..bfe0f76f7fb0 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -1780,4 +1780,102 @@ TEST_F(vfio_compat_mock_domain, huge_map) } } +FIXTURE(iommufd_device_resv_iova) +{ + int fd; + uint32_t ioas_id; + uint32_t stdev_id; + uint32_t device_id; +}; + +FIXTURE_SETUP(iommufd_device_resv_iova) +{ + self->fd = open("/dev/iommu", O_RDWR); + ASSERT_NE(-1, self->fd); + test_ioctl_ioas_alloc(&self->ioas_id); + + test_cmd_mock_domain(self->ioas_id, &self->stdev_id, + NULL, &self->device_id); +} + +FIXTURE_TEARDOWN(iommufd_device_resv_iova) +{ + teardown_iommufd(self->fd, _metadata); +} + +TEST_F(iommufd_device_resv_iova, dev_resv_iova_ranges) +{ + struct iommu_test_cmd test_cmd = { + .size = sizeof(test_cmd), + .op = IOMMU_TEST_OP_DEV_ADD_RESERVED, + .id = self->stdev_id, + .add_dev_reserved = { .start = PAGE_SIZE, .length = PAGE_SIZE }, + }; + struct iommu_test_cmd test_cmd_del = { + .size = sizeof(test_cmd_del), + .op = IOMMU_TEST_OP_DEV_DEL_RESERVED, + .id = self->stdev_id, + }; + struct iommu_resv_iova_range *ranges = buffer; + struct iommu_resv_iova_ranges ranges_cmd = { + .size = sizeof(ranges_cmd), + .dev_id = self->device_id, + .num_iovas = BUFFER_SIZE / sizeof(*ranges), + .resv_iovas = (uintptr_t)ranges, + }; + + /* Range can be read */ + ASSERT_EQ(0, ioctl(self->fd, IOMMU_RESV_IOVA_RANGES, &ranges_cmd)); + EXPECT_EQ(0, ranges_cmd.num_iovas); + + /* 1 range */ + ASSERT_EQ(0, + ioctl(self->fd, _IOMMU_TEST_CMD(IOMMU_TEST_OP_DEV_ADD_RESERVED), + &test_cmd)); + ranges_cmd.num_iovas = BUFFER_SIZE / sizeof(*ranges); + ASSERT_EQ(0, ioctl(self->fd, IOMMU_RESV_IOVA_RANGES, &ranges_cmd)); + EXPECT_EQ(1, ranges_cmd.num_iovas); + EXPECT_EQ(PAGE_SIZE, ranges[0].start); + EXPECT_EQ(PAGE_SIZE * 2 - 1, ranges[0].last); + + /* Buffer too small */ + memset(ranges, 0, BUFFER_SIZE); + ranges_cmd.num_iovas = 0; + EXPECT_ERRNO(EMSGSIZE, + ioctl(self->fd, IOMMU_RESV_IOVA_RANGES, &ranges_cmd)); + EXPECT_EQ(1, ranges_cmd.num_iovas); + EXPECT_EQ(0, ranges[0].start); + EXPECT_EQ(0, ranges[0].last); + + /* 2 ranges */ + test_cmd.add_dev_reserved.start = PAGE_SIZE * 4; + test_cmd.add_dev_reserved.length = PAGE_SIZE; + + ASSERT_EQ(0, + ioctl(self->fd, _IOMMU_TEST_CMD(IOMMU_TEST_OP_DEV_ADD_RESERVED), + &test_cmd)); + ranges_cmd.num_iovas = BUFFER_SIZE / sizeof(*ranges); + ASSERT_EQ(0, ioctl(self->fd, IOMMU_RESV_IOVA_RANGES, &ranges_cmd)); + EXPECT_EQ(2, ranges_cmd.num_iovas); + EXPECT_EQ(PAGE_SIZE, ranges[0].start); + EXPECT_EQ(PAGE_SIZE * 2 - 1, ranges[0].last); + EXPECT_EQ(PAGE_SIZE * 4, ranges[1].start); + EXPECT_EQ(PAGE_SIZE * 5 - 1, ranges[1].last); + + /* Buffer too small */ + memset(ranges, 0, BUFFER_SIZE); + ranges_cmd.num_iovas = 1; + EXPECT_ERRNO(EMSGSIZE, ioctl(self->fd, IOMMU_RESV_IOVA_RANGES, + &ranges_cmd)); + EXPECT_EQ(2, ranges_cmd.num_iovas); + EXPECT_EQ(PAGE_SIZE, ranges[0].start); + EXPECT_EQ(PAGE_SIZE * 2 - 1, ranges[0].last); + EXPECT_EQ(0, ranges[1].start); + EXPECT_EQ(0, ranges[1].last); + + ASSERT_EQ(0, + ioctl(self->fd, _IOMMU_TEST_CMD(IOMMU_TEST_OP_DEV_DEL_RESERVED), + &test_cmd_del)); +} + TEST_HARNESS_MAIN From patchwork Mon Jul 24 11:04: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: 124865 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp1734864vqg; Mon, 24 Jul 2023 04:33:57 -0700 (PDT) X-Google-Smtp-Source: APBJJlESUhTQibfHYxSUyfVdQlvzL8wBY74Nx28FE/kjjLo26bAmQQf6KxkXUXLMSGZAsanq6sFW X-Received: by 2002:a05:6a00:99a:b0:681:9004:3a20 with SMTP id u26-20020a056a00099a00b0068190043a20mr12304909pfg.7.1690198437563; Mon, 24 Jul 2023 04:33:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690198437; cv=none; d=google.com; s=arc-20160816; b=U7z456ftjvNgE1y3bl2cmoIpT+Ic71iMTVw+d5WmLdAvHtv724gkYR2rnf7W54/gpm CqcjTPbJmAZnC4+raVrMPLeiaT+OJZUmPNgvrUltL8XxpAtgZwhfAaDbu2YGhT8mNgzI YRut2rib6zFhNEgpV6qiGaEqeRyqn4pWf4FPNGjs7niP2zNYkOi0+BzqqLMz6icJt/ko 6dWBXDAbb0ZL1yICeZwEW8V4MrQ9KXey66CfOSv8aVvrIbB7T/a2iGCXkmMZH8IM+Y/m h2S6VzMIvMQyi5Y1/AYLnPBTLMu9cj67++ong52LbPd70fCnu6A1dpC//f6LjUvlaX5a EA0A== 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=h9rrd8Q1bOBJ+v4NnefIrzNYWzwtQCpiaUn6UdsDTeg=; fh=25kVo4cotgMzE250R6E3K5ES7E7C2JawiavlW0h5mKU=; b=ePDzu3EiAYHsSpmekBQjjmjnj6zwhhuRGAOej/6jh1SLk7Qi9R175G4ccb9n7SJY21 EUNBDlkg2jqsLXIuM2fVeM8zqlj/FFYGk4BhVQfnY2DAy0QgDa2sRxsHnD9OkNGZe+pN EV3BkKwCt/yntPLOVnu4JykgZD8xeSYWlEYLZAdcqA0d3mwEf7/xs8Vt0DBLLE5LgsaM 9tYlHs4+szsRoamhcnlHvqstIhaYQ+ZOb/hG+A5PM7p6H7l/GpEd5W5vgWEdgYw9BPZe oiyni3wYbgT5k7PoKMwy9E1EYRWv1KpTV+OxwvosFDKI7YJIhHREtjQeXvZrOQxKedCD GbHQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="V/KHmdiL"; 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 70-20020a630149000000b0055339507f67si8535850pgb.836.2023.07.24.04.33.44; Mon, 24 Jul 2023 04:33:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="V/KHmdiL"; 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 S233467AbjGXLFu (ORCPT + 99 others); Mon, 24 Jul 2023 07:05:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58296 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232944AbjGXLFc (ORCPT ); Mon, 24 Jul 2023 07:05:32 -0400 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D1BCA10CB; Mon, 24 Jul 2023 04:05:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690196710; x=1721732710; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=iWEG8B8IVLnnYXlFA9j627UTPjfHZtc7JFjCLROvnNw=; b=V/KHmdiL4FHYlrcSdo09JKgq4XydCqFcHbuZ+VQwU6WckJLE9h+3JNHr 77wPIfApLvytuh8b118CVzfQkCfRLw9gC8GanejvYoCmDjdd5PQpkKzNJ 8mZiYZCjcWHhHW0GIAn7MMx+yAAronU4hbtLY8XSvwGWz1RyEacjdbtum 0Fkq5qbujzidypukdPSFixFYhaOk3cj+3HUAVbwiQw3WS+RtdP+El/HIa VqdLxVYxoRr7XYoNWFc6JN9BVPkzAuP11vEAS3Xn5yU9N7BMb03LLOLJr mocfuBwpz9vsQFNqN98NVVySsNlCAzA7MqGFNgjZFlsL0Z0j99K/6lD5a A==; X-IronPort-AV: E=McAfee;i="6600,9927,10780"; a="347013340" X-IronPort-AV: E=Sophos;i="6.01,228,1684825200"; d="scan'208";a="347013340" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jul 2023 04:04:25 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10780"; a="815775882" X-IronPort-AV: E=Sophos;i="6.01,228,1684825200"; d="scan'208";a="815775882" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by FMSMGA003.fm.intel.com with ESMTP; 24 Jul 2023 04:04:23 -0700 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com Subject: [PATCH v3 14/17] iommufd/selftest: Add domain_alloc_user() support in iommu mock Date: Mon, 24 Jul 2023 04:04:03 -0700 Message-Id: <20230724110406.107212-15-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230724110406.107212-1-yi.l.liu@intel.com> References: <20230724110406.107212-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, SPF_HELO_PASS,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772301516686434748 X-GMAIL-MSGID: 1772301516686434748 From: Nicolin Chen Add mock_domain_alloc_user function and iommu_hwpt_selftest data structure to support user space selftest program to allocate domains with user data. Signed-off-by: Nicolin Chen Signed-off-by: Yi Liu --- drivers/iommu/iommufd/hw_pagetable.c | 1 + drivers/iommu/iommufd/iommufd_test.h | 16 ++++++ drivers/iommu/iommufd/selftest.c | 84 +++++++++++++++++++++++++--- include/linux/iommu.h | 3 + 4 files changed, 95 insertions(+), 9 deletions(-) diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index 9064e6d181b4..e30fce5d553b 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -7,6 +7,7 @@ #include "../iommu-priv.h" #include "iommufd_private.h" +#include "iommufd_test.h" void iommufd_hw_pagetable_destroy(struct iommufd_object *obj) { diff --git a/drivers/iommu/iommufd/iommufd_test.h b/drivers/iommu/iommufd/iommufd_test.h index b7f4bee2ea1b..eeac8100844d 100644 --- a/drivers/iommu/iommufd/iommufd_test.h +++ b/drivers/iommu/iommufd/iommufd_test.h @@ -115,4 +115,20 @@ struct iommu_test_hw_info { __u32 test_reg; }; +/* Should not be equal to any defined value in enum iommu_hwpt_type */ +#define IOMMU_HWPT_TYPE_SELFTTEST 0xdead + +/** + * struct iommu_hwpt_selftest + * + * @flags: page table entry attributes + * @test_config: default iotlb setup (value IOMMU_TEST_IOTLB_DEFAULT) + */ +struct iommu_hwpt_selftest { +#define IOMMU_TEST_FLAG_NESTED (1ULL << 0) + __u64 flags; +#define IOMMU_TEST_IOTLB_DEFAULT 0xbadbeef + __u64 test_config; +}; + #endif diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c index e40780db88b5..8364003efde3 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 { @@ -144,26 +146,81 @@ 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; + return ERR_PTR(-ENOMEM); + 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) +{ + struct iommu_domain *domain; + + if (WARN_ON(iommu_domain_type != IOMMU_DOMAIN_BLOCKED && + iommu_domain_type != IOMMU_DOMAIN_UNMANAGED)) + return NULL; + domain = __mock_domain_alloc(IOMMU_DOMAIN_UNMANAGED, NULL, NULL); + if (IS_ERR(domain)) + domain = NULL; + return domain; +} + +static struct iommu_domain *mock_domain_alloc_user(struct device *dev, + enum iommu_hwpt_type hwpt_type, + struct iommu_domain *parent, + const union iommu_domain_user_data *user_data) +{ + const struct iommu_hwpt_selftest *user_cfg = (struct iommu_hwpt_selftest *)user_data; + unsigned int iommu_domain_type = IOMMU_DOMAIN_UNMANAGED; + struct mock_iommu_domain *mock_parent = NULL; + + /* Check union iommu_domain_user_data in include/linux/iommu.h */ + static_assert(sizeof(struct iommu_hwpt_selftest) <= 16); + + if (hwpt_type != IOMMU_HWPT_TYPE_DEFAULT && + hwpt_type != IOMMU_HWPT_TYPE_SELFTTEST) + return ERR_PTR(-EINVAL); + + if (parent) { + if (hwpt_type != IOMMU_HWPT_TYPE_SELFTTEST) + return ERR_PTR(-EINVAL); + if (parent->ops != mock_ops.default_domain_ops) + return ERR_PTR(-EINVAL); + if (!user_cfg || !(user_cfg->flags & IOMMU_TEST_FLAG_NESTED)) + return ERR_PTR(-EINVAL); + 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 = @@ -324,6 +381,7 @@ static const struct iommu_ops mock_ops = { .hw_info_type = IOMMU_HW_INFO_TYPE_SELFTEST, .hw_info = mock_domain_hw_info, .domain_alloc = mock_domain_alloc, + .domain_alloc_user = mock_domain_alloc_user, .capable = mock_domain_capable, .get_resv_regions = iommufd_test_get_resv_regions, .set_platform_dma_ops = mock_domain_set_plaform_dma_ops, @@ -337,6 +395,11 @@ static const struct iommu_ops mock_ops = { }, }; +static struct iommu_domain_ops domain_nested_ops = { + .free = mock_domain_free, + .attach_dev = mock_domain_nop_attach, +}; + static struct iommu_device mock_iommu_device = { .ops = &mock_ops, }; @@ -353,7 +416,10 @@ get_md_pagetable(struct iommufd_ucmd *ucmd, u32 mockpt_id, if (IS_ERR(obj)) return ERR_CAST(obj); hwpt = container_of(obj, struct iommufd_hw_pagetable, obj); - if (hwpt->domain->ops != mock_ops.default_domain_ops) { + if ((hwpt->domain->type == IOMMU_DOMAIN_UNMANAGED && + hwpt->domain->ops != mock_ops.default_domain_ops) || + (hwpt->domain->type == IOMMU_DOMAIN_NESTED && + hwpt->domain->ops != &domain_nested_ops)) { iommufd_put_object(&hwpt->obj); return ERR_PTR(-EINVAL); } diff --git a/include/linux/iommu.h b/include/linux/iommu.h index b8f09330b64e..e4835230d5f0 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -237,6 +237,9 @@ struct iommu_iotlb_gather { * include/uapi/linux/iommufd.h. */ union iommu_domain_user_data { +#ifdef CONFIG_IOMMUFD_TEST + __u64 test[2]; +#endif }; /** From patchwork Mon Jul 24 11:04: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: 124840 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp1726926vqg; Mon, 24 Jul 2023 04:18:50 -0700 (PDT) X-Google-Smtp-Source: APBJJlEsWpIpCrRUpwdaFpXqgKL3hdX4YPE7OHT6kz5cp/KGyEXxd2SeVXkdDAzFL4wJQMZ0Kd6+ X-Received: by 2002:a05:6402:1355:b0:51e:22dd:5e90 with SMTP id y21-20020a056402135500b0051e22dd5e90mr7778369edw.4.1690197530298; Mon, 24 Jul 2023 04:18:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690197530; cv=none; d=google.com; s=arc-20160816; b=vw3Ol6y7D3KVyZKhPbZNtQ++dqRmmFlZfYZkKml1WpFJlFAPSWEy98SzMwhGNviyap HZmSWAoOMGQyhrXlNqZMN+N9IOmL+kJJWPLcZZ8tIMXO/MTijtkDiNb178VhVgVyn/xW XY1m7V44RCnxCc3N3W3/2rDJFGMeVR4I0+wSZ2IF/YF8OefEtH1YSjLiY3bti3QinSWA 31E+ea+oWMq/+iOQEbmm/aIyuSTrWzqiH9E3xHLIVAaoKJY4TyyFo3RlFvRWcRLFSnEd iITYL3gSvJqC5I8engiTrK8++7lef7eFv7claS0Mg0tx6/p3PFcR+ESEo568DN8EwjhR lsYg== 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=2BOrFWKoIB8/oQyqNGTFxhSXq9AV7q02/80d0doYXqY=; fh=25kVo4cotgMzE250R6E3K5ES7E7C2JawiavlW0h5mKU=; b=pD/3vwLpoiDbVE1P7+T5uFJOfp5A3W5qAkh2czsJziFDvM4W0Nzup/FX7+Uzcu88Yv QXYWxajyUhDd/UYzx5zri1wJjCmkNLmwBNJ7YRkA4XSev3YVcPJrONXWWs6wj465nhs0 jRIG7gHztqkYIwUSHj6Dv8TRLgVdRjevZBA3Zr8bDpo0VxQA1Cd3wjdkDFyKtUgE0C8E rKYvvXbKuEN+Bse2UfW7YSuEZpnLJd0Xx8y379PsLYfApENYu2+vl3mDLG1Wx5ULoOcW YI8NRuqip7KsNig81eL9d4xJpB138rRf/gO2m+lL1zz1EcuGxEAwifx/7mFPkejlEEGf IQTQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=J4xwSdFH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k24-20020aa7c058000000b0051bdf1abe3csi6108762edo.415.2023.07.24.04.18.23; Mon, 24 Jul 2023 04:18:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=J4xwSdFH; 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 S233431AbjGXLFm (ORCPT + 99 others); Mon, 24 Jul 2023 07:05:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59422 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233452AbjGXLF2 (ORCPT ); Mon, 24 Jul 2023 07:05:28 -0400 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4877DE55; Mon, 24 Jul 2023 04:05:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690196703; x=1721732703; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=MEDTFzJSWH7Dn8MTQLtqIwIGcW191lhpinBFtTwSFsY=; b=J4xwSdFHCRBVjBwqIDmxGEmhfq0X+ZxPwflOXBMPY1KVpEVG8qrDeOT7 msDS9asAU708XvMur7LBpBU5VYn9MyuBdmzsB7HKeumeLrqxZZSf+qgGZ 6kWGcO2eJYMWwDsu1iCuZJPOHLaVrN+iuyzmiJ/2Rx6+qBZkG1KL2Q0XY GWJYanaH4OwmF2lA5Ncx3WkvUCRsYFopa6dX+8ofKxIdYVJ/S7n1LT/vc HgCylLgechrY0mxMvrbgIx2PTbv9wz8ek+dns6e9sZ7koYnhaJm6HXGPh oH8nzilw7uztcynitc9vvVzhNWOVFMNsWMaajOvQKdCMMnvUY46nqA+7P w==; X-IronPort-AV: E=McAfee;i="6600,9927,10780"; a="347013342" X-IronPort-AV: E=Sophos;i="6.01,228,1684825200"; d="scan'208";a="347013342" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jul 2023 04:04:25 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10780"; a="815775888" X-IronPort-AV: E=Sophos;i="6.01,228,1684825200"; d="scan'208";a="815775888" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by FMSMGA003.fm.intel.com with ESMTP; 24 Jul 2023 04:04:24 -0700 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com Subject: [PATCH v3 15/17] iommufd/selftest: Add coverage for IOMMU_HWPT_ALLOC with user data Date: Mon, 24 Jul 2023 04:04:04 -0700 Message-Id: <20230724110406.107212-16-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230724110406.107212-1-yi.l.liu@intel.com> References: <20230724110406.107212-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, SPF_HELO_PASS,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772300565618685707 X-GMAIL-MSGID: 1772300565618685707 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 bfe0f76f7fb0..70116c2dfc94 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -114,6 +114,7 @@ TEST_F(iommufd, cmd_length) TEST_LENGTH(iommu_destroy, IOMMU_DESTROY); TEST_LENGTH(iommu_hw_info, IOMMU_GET_HW_INFO); + TEST_LENGTH(iommu_hwpt_alloc, IOMMU_HWPT_ALLOC); TEST_LENGTH(iommu_ioas_alloc, IOMMU_IOAS_ALLOC); TEST_LENGTH(iommu_ioas_iova_ranges, IOMMU_IOAS_IOVA_RANGES); TEST_LENGTH(iommu_ioas_allow_iovas, IOMMU_IOAS_ALLOW_IOVAS); @@ -194,6 +195,7 @@ FIXTURE_VARIANT(iommufd_ioas) { unsigned int mock_domains; unsigned int memory_limit; + bool new_hwpt; }; FIXTURE_SETUP(iommufd_ioas) @@ -233,6 +235,12 @@ FIXTURE_VARIANT_ADD(iommufd_ioas, mock_domain) .mock_domains = 1, }; +FIXTURE_VARIANT_ADD(iommufd_ioas, mock_domain_hwpt) +{ + .mock_domains = 1, + .new_hwpt = true, +}; + FIXTURE_VARIANT_ADD(iommufd_ioas, two_mock_domain) { .mock_domains = 2, @@ -260,6 +268,93 @@ TEST_F(iommufd_ioas, ioas_destroy) } } +TEST_F(iommufd_ioas, hwpt_alloc) +{ + uint32_t new_hwpt_id = 0; + + if (self->stdev_id && self->device_id) { + test_cmd_hwpt_alloc(self->device_id, self->ioas_id, &new_hwpt_id); + test_cmd_mock_domain_replace(self->stdev_id, new_hwpt_id); + /* hw_pagetable cannot be freed if a device is attached to it */ + EXPECT_ERRNO(EBUSY, _test_ioctl_destroy(self->fd, new_hwpt_id)); + + /* Detach from the new hw_pagetable and try again */ + test_cmd_mock_domain_replace(self->stdev_id, self->ioas_id); + test_ioctl_destroy(new_hwpt_id); + } else { + test_err_cmd_hwpt_alloc(ENOENT, self->device_id, + self->ioas_id, &new_hwpt_id); + test_err_mock_domain_replace(ENOENT, + self->stdev_id, new_hwpt_id); + } +} + +TEST_F(iommufd_ioas, nested_hwpt_alloc) +{ + uint32_t nested_hwpt_id[2] = {}; + uint32_t parent_hwpt_id = 0; + uint32_t test_hwpt_id = 0; + + if (self->device_id) { + /* Negative tests */ + test_err_cmd_hwpt_alloc(ENOENT, self->ioas_id, self->device_id, + &test_hwpt_id); + test_err_cmd_hwpt_alloc(EINVAL, self->device_id, + self->device_id, &test_hwpt_id); + + /* Allocate two nested hwpts sharing one common parent hwpt */ + test_cmd_hwpt_alloc(self->device_id, self->ioas_id, + &parent_hwpt_id); + + test_cmd_hwpt_alloc_nested(self->device_id, parent_hwpt_id, + &nested_hwpt_id[0]); + test_cmd_hwpt_alloc_nested(self->device_id, parent_hwpt_id, + &nested_hwpt_id[1]); + + /* Negative test: a nested hwpt on top of a nested hwpt */ + test_err_cmd_hwpt_alloc_nested(EINVAL, self->device_id, + nested_hwpt_id[0], + &test_hwpt_id); + /* Negative test: parent hwpt now cannot be freed */ + EXPECT_ERRNO(EBUSY, + _test_ioctl_destroy(self->fd, parent_hwpt_id)); + + /* Attach device to nested_hwpt_id[0] that then will be busy */ + test_cmd_mock_domain_replace(self->stdev_id, + nested_hwpt_id[0]); + EXPECT_ERRNO(EBUSY, + _test_ioctl_destroy(self->fd, nested_hwpt_id[0])); + + /* Switch from nested_hwpt_id[0] to nested_hwpt_id[1] */ + test_cmd_mock_domain_replace(self->stdev_id, + nested_hwpt_id[1]); + EXPECT_ERRNO(EBUSY, + _test_ioctl_destroy(self->fd, nested_hwpt_id[1])); + test_ioctl_destroy(nested_hwpt_id[0]); + + /* Detach from nested_hwpt_id[1] and destroy it */ + test_cmd_mock_domain_replace(self->stdev_id, parent_hwpt_id); + test_ioctl_destroy(nested_hwpt_id[1]); + + /* Detach from the parent hw_pagetable and destroy it */ + test_cmd_mock_domain_replace(self->stdev_id, self->ioas_id); + test_ioctl_destroy(parent_hwpt_id); + } else { + test_err_cmd_hwpt_alloc(ENOENT, self->device_id, self->ioas_id, + &parent_hwpt_id); + test_err_cmd_hwpt_alloc_nested(ENOENT, self->device_id, + parent_hwpt_id, + &nested_hwpt_id[0]); + test_err_cmd_hwpt_alloc_nested(ENOENT, self->device_id, + parent_hwpt_id, + &nested_hwpt_id[1]); + test_err_mock_domain_replace(ENOENT, self->stdev_id, + nested_hwpt_id[0]); + test_err_mock_domain_replace(ENOENT, self->stdev_id, + nested_hwpt_id[1]); + } +} + TEST_F(iommufd_ioas, hwpt_attach) { /* Create a device attached directly to a hwpt */ @@ -663,6 +758,8 @@ TEST_F(iommufd_ioas, access_pin) MOCK_FLAGS_ACCESS_CREATE_NEEDS_PIN_PAGES); for (npages = 1; npages < BUFFER_SIZE / PAGE_SIZE; npages++) { + uint32_t new_hwpt_id = 0; + uint32_t mock_device_id; uint32_t mock_stdev_id; uint32_t mock_hwpt_id; @@ -696,12 +793,27 @@ TEST_F(iommufd_ioas, access_pin) _IOMMU_TEST_CMD(IOMMU_TEST_OP_ACCESS_PAGES), &access_cmd)); test_cmd_mock_domain(self->ioas_id, &mock_stdev_id, - &mock_hwpt_id, NULL); + &mock_hwpt_id, &mock_device_id); check_map_cmd.id = mock_hwpt_id; + if (variant->new_hwpt) { + test_cmd_hwpt_alloc(mock_device_id, self->ioas_id, + &new_hwpt_id); + test_cmd_mock_domain_replace(mock_stdev_id, + new_hwpt_id); + check_map_cmd.id = new_hwpt_id; + } else { + check_map_cmd.id = mock_hwpt_id; + } ASSERT_EQ(0, ioctl(self->fd, _IOMMU_TEST_CMD(IOMMU_TEST_OP_MD_CHECK_MAP), &check_map_cmd)); + if (variant->new_hwpt) { + /* Detach from the new hwpt for its destroy() */ + test_cmd_mock_domain_replace(mock_stdev_id, + mock_hwpt_id); + test_ioctl_destroy(new_hwpt_id); + } test_ioctl_destroy(mock_stdev_id); test_cmd_destroy_access_pages( access_cmd.id, diff --git a/tools/testing/selftests/iommu/iommufd_utils.h b/tools/testing/selftests/iommu/iommufd_utils.h index f13df84f6b42..365fa3da2016 100644 --- a/tools/testing/selftests/iommu/iommufd_utils.h +++ b/tools/testing/selftests/iommu/iommufd_utils.h @@ -118,6 +118,42 @@ static int _test_cmd_hwpt_alloc(int fd, __u32 device_id, __u32 pt_id, #define test_cmd_hwpt_alloc(device_id, pt_id, hwpt_id) \ ASSERT_EQ(0, _test_cmd_hwpt_alloc(self->fd, device_id, pt_id, hwpt_id)) +#define test_err_cmd_hwpt_alloc(_errno, device_id, pt_id, hwpt_id) \ + EXPECT_ERRNO(_errno, _test_cmd_hwpt_alloc(self->fd, device_id, \ + pt_id, hwpt_id)) + +static int _test_cmd_hwpt_alloc_nested(int fd, __u32 device_id, __u32 parent_id, + __u32 *hwpt_id) +{ + struct iommu_hwpt_selftest data = { + .flags = IOMMU_TEST_FLAG_NESTED, + .test_config = IOMMU_TEST_IOTLB_DEFAULT, + }; + struct iommu_hwpt_alloc cmd = { + .size = sizeof(cmd), + .dev_id = device_id, + .pt_id = parent_id, + .hwpt_type = IOMMU_HWPT_TYPE_SELFTTEST, + .data_len = sizeof(data), + .data_uptr = (uint64_t)&data, + }; + int ret; + + ret = ioctl(fd, IOMMU_HWPT_ALLOC, &cmd); + if (ret) + return ret; + if (hwpt_id) + *hwpt_id = cmd.out_hwpt_id; + return 0; +} + +#define test_cmd_hwpt_alloc_nested(device_id, parent_id, hwpt_id) \ + ASSERT_EQ(0, _test_cmd_hwpt_alloc_nested(self->fd, device_id, \ + parent_id, hwpt_id)) +#define test_err_cmd_hwpt_alloc_nested(_errno, device_id, parent_id, hwpt_id) \ + EXPECT_ERRNO(_errno, \ + _test_cmd_hwpt_alloc_nested(self->fd, device_id, \ + parent_id, hwpt_id)) static int _test_cmd_access_replace_ioas(int fd, __u32 access_id, unsigned int ioas_id) From patchwork Mon Jul 24 11:04: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: 124839 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp1726814vqg; Mon, 24 Jul 2023 04:18:38 -0700 (PDT) X-Google-Smtp-Source: APBJJlFSocmIqST6KWOzNCdHu7EzWaqwhua9dmuXX9IwVRjW/vmoxPW6TQoV7YFd6TLCu6W9jRWr X-Received: by 2002:a05:6e02:1aa4:b0:348:ccb6:dc7c with SMTP id l4-20020a056e021aa400b00348ccb6dc7cmr5271364ilv.18.1690197518184; Mon, 24 Jul 2023 04:18:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690197518; cv=none; d=google.com; s=arc-20160816; b=BCtVGf7Q/Sm1bGdceUTqPwNvj58oTu7AdaY3JMMl+Q4NFweYf7XU8U9HWs4XPPU4Kv Iap8HmPrN3HO7xbUavDpn566JWwWplShP8taB3NuREOc33hpHTExK7fsohnIFOpzxRw7 DVLOTvj6i93ZGPjCVfgAHPfYhx8VafC7GEg1bM9VmyGvI3L8zhvyx3wzTcJs51DfdcuC 4SZhmwqDs+oGI6vtdW2xCv1hsfYmm5fMV49ggpF12+ENoLI12I6tWuR7v9op9ll2UHja kEMHYcDSi8qxuwcndXQ/qt0gmFqcRZeBAfL5Ale/zQjoBCMU1060Ggrcck3Vrhl8VxLQ zABw== 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=X/N20rgHnmpbWsgl4LXglviT7HuYIZ/iBvwMUEXngHo=; fh=25kVo4cotgMzE250R6E3K5ES7E7C2JawiavlW0h5mKU=; b=Pag+WyaDhwYGy6DAWPVjMnFWrzCopnQlabuCEBVgv6IhZVY0KntD/qVKBeqnoRy9l7 ECglXIkk8NJJZiE1HHo+hZ1Txg1trT5w6XEZninYwCuL7P3KshIqBjdjPmG2XCtbdPxJ 064BuFfyA246CIhG+pWAu0ZJCkthnm8UFDn5UyC85U4Jq0sEcYN9ovG8r0YSBMaEOKFV mcb6+Ee+eSGLSq1g/oUs9etbqSNGFVQNQBwSnqlacthjSeQfrZPZHKVdFTHPLrOXqzxz j/cBAqaIE2j3h1slap/nrmCV20+2DW5T4M91Ee0ywzNq3+DKAxavCwmV9tZWwtLHD8Qe 87Ow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=lPFvVkdP; 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 ea17-20020a056a004c1100b006797c2c00besi8792870pfb.144.2023.07.24.04.18.25; Mon, 24 Jul 2023 04:18:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=lPFvVkdP; 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 S233441AbjGXLGE (ORCPT + 99 others); Mon, 24 Jul 2023 07:06:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59178 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232167AbjGXLFm (ORCPT ); Mon, 24 Jul 2023 07:05:42 -0400 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BBF2F1BC3; Mon, 24 Jul 2023 04:05:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690196725; x=1721732725; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=A2LaVc0RXcdBJTdftWm15aTLtP5VHqA4B+E9J8h6wDI=; b=lPFvVkdPn6ncj2S4692/P+ZYJNuln/EvyPT9n3u1mTm5uFiyBqAB46Jf mBgwrj9woz/4SbKhApbJ2YP62u6QvPCgr8w5R06Qm7g0f0+Nc13f1iLho x82rKAeA65+7+dP/K+4TzBWVZ3hFreFETbGwfFxpLOtxfa/oYQe0RNY2/ tIh6DkaA/mkx4FhSGNtDywSZisGe5ZNmDkNUcKEXKwR8FjLJ0Lya0uJJh av9VTT6mXJRmibZpQUQpwgopbofLoQtafT+KQN2X0j5dEaGMqgCicIolq OoXdw6PikeRpHjPu3ArCS+9pms3VpbFsaY6+4UOuRpI7Di0Y5IHJNUsvp g==; X-IronPort-AV: E=McAfee;i="6600,9927,10780"; a="347013358" X-IronPort-AV: E=Sophos;i="6.01,228,1684825200"; d="scan'208";a="347013358" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jul 2023 04:04:26 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10780"; a="815775893" X-IronPort-AV: E=Sophos;i="6.01,228,1684825200"; d="scan'208";a="815775893" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by FMSMGA003.fm.intel.com with ESMTP; 24 Jul 2023 04:04:24 -0700 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com Subject: [PATCH v3 16/17] iommufd/selftest: Add IOMMU_TEST_OP_MD_CHECK_IOTLB test op Date: Mon, 24 Jul 2023 04:04:05 -0700 Message-Id: <20230724110406.107212-17-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230724110406.107212-1-yi.l.liu@intel.com> References: <20230724110406.107212-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, SPF_HELO_PASS,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772300552609593840 X-GMAIL-MSGID: 1772300552609593840 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 eeac8100844d..93fcbb57b904 100644 --- a/drivers/iommu/iommufd/iommufd_test.h +++ b/drivers/iommu/iommufd/iommufd_test.h @@ -21,6 +21,7 @@ enum { IOMMU_TEST_OP_ACCESS_REPLACE_IOAS, IOMMU_TEST_OP_DEV_ADD_RESERVED, IOMMU_TEST_OP_DEV_DEL_RESERVED, + IOMMU_TEST_OP_MD_CHECK_IOTLB, }; enum { @@ -101,6 +102,9 @@ struct iommu_test_cmd { struct { __u32 ioas_id; } access_replace_ioas; + struct { + __u32 iotlb; + } check_iotlb; }; __u32 last; }; diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c index 8364003efde3..92c37716804e 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -795,6 +795,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; @@ -1214,6 +1233,9 @@ int iommufd_test(struct iommufd_ucmd *ucmd) return iommufd_test_md_check_refs( ucmd, u64_to_user_ptr(cmd->check_refs.uptr), cmd->check_refs.length, cmd->check_refs.refs); + case IOMMU_TEST_OP_MD_CHECK_IOTLB: + return iommufd_test_md_check_iotlb(ucmd, cmd->id, + cmd->check_iotlb.iotlb); case IOMMU_TEST_OP_CREATE_ACCESS: return iommufd_test_create_access(ucmd, cmd->id, cmd->create_access.flags); diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c index 70116c2dfc94..942860365cb6 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -310,6 +310,10 @@ TEST_F(iommufd_ioas, nested_hwpt_alloc) &nested_hwpt_id[0]); test_cmd_hwpt_alloc_nested(self->device_id, parent_hwpt_id, &nested_hwpt_id[1]); + test_cmd_hwpt_check_iotlb(nested_hwpt_id[0], + IOMMU_TEST_IOTLB_DEFAULT); + test_cmd_hwpt_check_iotlb(nested_hwpt_id[1], + IOMMU_TEST_IOTLB_DEFAULT); /* Negative test: a nested hwpt on top of a nested hwpt */ test_err_cmd_hwpt_alloc_nested(EINVAL, self->device_id, diff --git a/tools/testing/selftests/iommu/iommufd_utils.h b/tools/testing/selftests/iommu/iommufd_utils.h index 365fa3da2016..5e75dfac65e9 100644 --- a/tools/testing/selftests/iommu/iommufd_utils.h +++ b/tools/testing/selftests/iommu/iommufd_utils.h @@ -122,6 +122,20 @@ static int _test_cmd_hwpt_alloc(int fd, __u32 device_id, __u32 pt_id, EXPECT_ERRNO(_errno, _test_cmd_hwpt_alloc(self->fd, device_id, \ pt_id, hwpt_id)) +#define test_cmd_hwpt_check_iotlb(hwpt_id, expected) \ + ({ \ + struct iommu_test_cmd test_cmd = { \ + .size = sizeof(test_cmd), \ + .op = IOMMU_TEST_OP_MD_CHECK_IOTLB, \ + .id = hwpt_id, \ + .check_iotlb = { .iotlb = expected }, \ + }; \ + ASSERT_EQ(0, \ + ioctl(self->fd, \ + _IOMMU_TEST_CMD(IOMMU_TEST_OP_MD_CHECK_IOTLB), \ + &test_cmd)); \ + }) + static int _test_cmd_hwpt_alloc_nested(int fd, __u32 device_id, __u32 parent_id, __u32 *hwpt_id) { From patchwork Mon Jul 24 11:04: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: 124877 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp1739269vqg; Mon, 24 Jul 2023 04:43:03 -0700 (PDT) X-Google-Smtp-Source: APBJJlGp/UFqYs+/rVQvWNC6Oz7WzhpCGcxI9HnkX0nd7vUqS5iuqlfIpZ1QoJTtGMTh6gTskjtH X-Received: by 2002:a17:902:ba91:b0:1bb:673f:36ae with SMTP id k17-20020a170902ba9100b001bb673f36aemr7015775pls.15.1690198983610; Mon, 24 Jul 2023 04:43:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690198983; cv=none; d=google.com; s=arc-20160816; b=S0gci3ne1UELWe7K46qk3OJ7hEpUzkrBXSyfMxH3fWPrlgZZAzd6BN+XA71scB0JQb kd/MZcXpJFa9Blu0N+WGiG/Jq/iH+3hHrDAp2UPzNIjpflf1tfe+jKaieCl8wuJxKc+p k0+FJtuggdi5T2H1ViZ0nfRYGYzPalPvhHidBmN9AhpFzlSAT0UhLV0j1/vzhrwOIXWV 9xXNOnr0yPzk6SSmECF1WYrl1BoeIzbO93gZp2tWOVIA3IrQ+eQuahcUd2A+WfAWWLPE wF1952q19hOpp+7f2k/aNDrJqRVUwsEc5ZR50KKISDQQ9i5I7+Lb5LMDrmK+uz/UcQC+ KRjA== 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=p1xuHMePAAyO7zFoUnQE8wzVXF8V2JBaBShfKk9ntkE=; fh=25kVo4cotgMzE250R6E3K5ES7E7C2JawiavlW0h5mKU=; b=F/P2pfOaKSGz+yWluDb7JgzO2I9bZUb80co03hFfOo0JEAhQ6U/3L2Bq4BdHGi2wWp n6AeK4qwgCD1pSEZxXlrnI40iNokGgIXD8HFYw1z4toFn7vphhN2ikaFgiihPA1BSaXA 85T00WYrarA79TDz+bwoOnguiUlwLTb37iB8i/CQNFYXLYcDF+iQFRvNa/Y7xhCMWKmw sUc4OCJ74y9PpgzW21tfY78/Ql08OlrpBS9k+4O9F/7d9Qhrt5ZMot9O4223I//rkpoi DsMHRc7p3Vkjosups075/ZFyYk3O2lDxbDuR9Lb3r05uwfrSz/+plV4u72VNonmFZVYT x2uw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=nL7rLgVF; 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 z9-20020a170903018900b001b3cf7eb499si9530961plg.633.2023.07.24.04.42.48; Mon, 24 Jul 2023 04:43:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=nL7rLgVF; 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 S231428AbjGXLGA (ORCPT + 99 others); Mon, 24 Jul 2023 07:06:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59146 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232170AbjGXLFl (ORCPT ); Mon, 24 Jul 2023 07:05:41 -0400 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6121A19BF; Mon, 24 Jul 2023 04:05:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690196725; x=1721732725; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Vlk7hKfSz0vf7ls07gWGHz24yNzmZ+WhjoD4+B5vePM=; b=nL7rLgVFzr2GMXBIpT9DIkwt+JSL/1fTEOlrG5p5z3APTSmj8QZiYWk2 b0ihnwm4vfxfCsY3k00al0cd/Q5AjNd9NrHxmVqgaPBxMn7//3fIgD+ip V3GolOVo8AVhALMTZojfDTudGI94aw6AGbarVNWC9Sl1ZoMLow0Gqy8Sd Sf/00TqNArz2Sv0PI8V+xwZhqsC+J8U0+HreQCnYRFq5tDCOgSvzp59KB 32niPuX1AYXWFU2K7Fi/oRJSXMNvQzISEIEhTqm/djMEK+K7XT80f4T58 TyhlJ6cNNxxxnWyOFuMLijtUVdCyugrZQRNKnblf61hEO+ZBz2NYTNdFU w==; X-IronPort-AV: E=McAfee;i="6600,9927,10780"; a="347013370" X-IronPort-AV: E=Sophos;i="6.01,228,1684825200"; d="scan'208";a="347013370" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jul 2023 04:04:26 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10780"; a="815775899" X-IronPort-AV: E=Sophos;i="6.01,228,1684825200"; d="scan'208";a="815775899" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by FMSMGA003.fm.intel.com with ESMTP; 24 Jul 2023 04:04:25 -0700 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com Subject: [PATCH v3 17/17] iommufd/selftest: Add coverage for IOMMU_HWPT_INVALIDATE ioctl Date: Mon, 24 Jul 2023 04:04:06 -0700 Message-Id: <20230724110406.107212-18-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230724110406.107212-1-yi.l.liu@intel.com> References: <20230724110406.107212-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, SPF_HELO_PASS,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772302089265154658 X-GMAIL-MSGID: 1772302089265154658 From: Nicolin Chen Add a mock_domain_cache_invalidate_user() and a corresponding struct iommu_hwpt_invalidate_selftest, to allow to test IOMMU_HWPT_INVALIDATE from user space, by using the new IOMMU_TEST_OP_MD_CHECK_IOTLB. Signed-off-by: Nicolin Chen Signed-off-by: Yi Liu --- drivers/iommu/iommufd/iommufd_test.h | 10 ++++++++++ drivers/iommu/iommufd/selftest.c | 19 ++++++++++++++++++ tools/testing/selftests/iommu/iommufd.c | 8 ++++++++ tools/testing/selftests/iommu/iommufd_utils.h | 20 +++++++++++++++++++ 4 files changed, 57 insertions(+) diff --git a/drivers/iommu/iommufd/iommufd_test.h b/drivers/iommu/iommufd/iommufd_test.h index 93fcbb57b904..9768d0c9e347 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 92c37716804e..5f3e1f2a24e7 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -395,9 +395,28 @@ static const struct iommu_ops mock_ops = { }, }; +static int mock_domain_cache_invalidate_user(struct iommu_domain *domain, + void *user_data) +{ + struct iommu_hwpt_invalidate_selftest *inv_info = user_data; + struct mock_iommu_domain *mock = + container_of(domain, struct mock_iommu_domain, domain); + + if (domain->type != IOMMU_DOMAIN_NESTED || !mock->parent) + return -EINVAL; + + if (inv_info->flags & IOMMU_TEST_INVALIDATE_ALL) + mock->iotlb = 0; + + return 0; +} + static struct iommu_domain_ops domain_nested_ops = { .free = mock_domain_free, .attach_dev = mock_domain_nop_attach, + .cache_invalidate_user = mock_domain_cache_invalidate_user, + .cache_invalidate_user_data_len = + sizeof(struct iommu_hwpt_invalidate_selftest), }; static struct iommu_device mock_iommu_device = { diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c index 942860365cb6..bf0082d88a38 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -115,6 +115,7 @@ TEST_F(iommufd, cmd_length) TEST_LENGTH(iommu_destroy, IOMMU_DESTROY); TEST_LENGTH(iommu_hw_info, IOMMU_GET_HW_INFO); TEST_LENGTH(iommu_hwpt_alloc, IOMMU_HWPT_ALLOC); + TEST_LENGTH(iommu_hwpt_invalidate, IOMMU_HWPT_INVALIDATE); TEST_LENGTH(iommu_ioas_alloc, IOMMU_IOAS_ALLOC); TEST_LENGTH(iommu_ioas_iova_ranges, IOMMU_IOAS_IOVA_RANGES); TEST_LENGTH(iommu_ioas_allow_iovas, IOMMU_IOAS_ALLOW_IOVAS); @@ -323,6 +324,13 @@ TEST_F(iommufd_ioas, nested_hwpt_alloc) EXPECT_ERRNO(EBUSY, _test_ioctl_destroy(self->fd, parent_hwpt_id)); + /* hwpt_invalidate only supports a user-managed hwpt (nested) */ + test_err_cmd_hwpt_invalidate(EINVAL, parent_hwpt_id); + test_cmd_hwpt_invalidate(nested_hwpt_id[0]); + test_cmd_hwpt_check_iotlb(nested_hwpt_id[0], 0); + test_cmd_hwpt_invalidate(nested_hwpt_id[1]); + test_cmd_hwpt_check_iotlb(nested_hwpt_id[1], 0); + /* Attach device to nested_hwpt_id[0] that then will be busy */ test_cmd_mock_domain_replace(self->stdev_id, nested_hwpt_id[0]); diff --git a/tools/testing/selftests/iommu/iommufd_utils.h b/tools/testing/selftests/iommu/iommufd_utils.h index 5e75dfac65e9..9b3e5f36c4a3 100644 --- a/tools/testing/selftests/iommu/iommufd_utils.h +++ b/tools/testing/selftests/iommu/iommufd_utils.h @@ -169,6 +169,26 @@ static int _test_cmd_hwpt_alloc_nested(int fd, __u32 device_id, __u32 parent_id, _test_cmd_hwpt_alloc_nested(self->fd, device_id, \ parent_id, hwpt_id)) +static int _test_cmd_hwpt_invalidate(int fd, __u32 hwpt_id) +{ + struct iommu_hwpt_invalidate_selftest data = { + .flags = IOMMU_TEST_INVALIDATE_ALL, + }; + struct iommu_hwpt_invalidate cmd = { + .size = sizeof(cmd), + .hwpt_id = hwpt_id, + .data_len = sizeof(data), + .data_uptr = (uint64_t)&data, + }; + + return ioctl(fd, IOMMU_HWPT_INVALIDATE, &cmd); +} + +#define test_cmd_hwpt_invalidate(hwpt_id) \ + ASSERT_EQ(0, _test_cmd_hwpt_invalidate(self->fd, hwpt_id)) +#define test_err_cmd_hwpt_invalidate(_errno, hwpt_id) \ + EXPECT_ERRNO(_errno, _test_cmd_hwpt_invalidate(self->fd, hwpt_id)) + static int _test_cmd_access_replace_ioas(int fd, __u32 access_id, unsigned int ioas_id) {