From patchwork Thu Feb 9 04:16:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 54703 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp119241wrn; Wed, 8 Feb 2023 20:25:25 -0800 (PST) X-Google-Smtp-Source: AK7set8DsxsCKXx/A9JPqyNonfCWBrviPQ8LiXp8je+GykHTSzDvpAsWMjMYVtWVewEcsehoQCbJ X-Received: by 2002:a17:906:36d9:b0:870:b950:18d4 with SMTP id b25-20020a17090636d900b00870b95018d4mr9842503ejc.5.1675916725285; Wed, 08 Feb 2023 20:25:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675916725; cv=none; d=google.com; s=arc-20160816; b=BRa94HXCmvJulnN0vL5LyRx4jtFvy/q1Q6lrzzOUbTHY+Cd3q7kiDb+eKPvTDYgorK yfxBCtrqU7yM/VHtHxPhpFlC77ZjwlUy/hpr14X/gwjPcZTvc3ALuSEvsFD2m6K+hide mcv/P05wk/iV+M64mU1nZbnNweNTbtws0gpURndpPmdpiqqoThR7sZv7OG/OQuh9la1H iwk54P4t8eFIls1sVxYzNdNeeqPbEouWg5adN2QhFK1TOH6dNSAOMUaMJjpDG1lOnbYj Er/ASx06i2rR4fv+eA4F7iZuNyyZxnlqPwG4j5Y2SO7pds0dYN+PjuEW/13yn4ImPfIj 3MxQ== 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=hjWSK7UruQVaRWIlkV2xVAYap7M5RLxgZvtgZGdypPo=; b=P7Ord4rjYbMzHBAlq2+fYuJC6YxCvtFjlKCRtN34MkH/deNOPDECmBa+dXuTLH8LrS cyB6rjyl5xeJzflW65YbSwX6dM15IeZUscLKVEPTag5eJYkS4Bk4RwbrUXH+hfpgQdkO MAey3GDy/w/fHAcw71avVZvO/IOzWx2ZxBfwLxDxb81LGOyMb41KU+WZY3SoG9wzqhBP rqSmRfdZbUvbaJgTga1svQRheZsvlP95SECOOFpxMKl3Qal1Le9cvUtOH80kZcqXNA1t 7mE+Fg91hW/K1BBr1nsBlVJrOlNRBrZQX89OQdOKdAeMaw390/ik4BDOY6deVVD041ts Fyng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=D538TFeN; 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 z23-20020aa7d417000000b004aadabcf7c8si930052edq.78.2023.02.08.20.25.02; Wed, 08 Feb 2023 20:25:25 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=D538TFeN; 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 S229576AbjBIETa (ORCPT + 99 others); Wed, 8 Feb 2023 23:19:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33296 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229478AbjBIETY (ORCPT ); Wed, 8 Feb 2023 23:19:24 -0500 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D5E4D10242; Wed, 8 Feb 2023 20:18:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675916308; x=1707452308; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=yLfWIPfMZf4ex+xdPNvOR4QIAocyXgXzIiZTA67s30A=; b=D538TFeNArPwCJ3B2b+VXX2e33wRjGsR6l0o5tmgmjSst0/YJhbi1jmp nxr4uyN6P51gcB53b+9GjfT1ctKxALXC8iQ01ZSbAsC0Pw84tybaBnTg/ hI8gI/0jNsOp6z0/FCFzbJM+oLO8kepQpMkGdfHZAmDspZxM/3lgb2Qtu uK6ZTV8PjeKRro+rtCuh9isTP7OSOIrLVqmeX05leBIUkhjzRyadimrWx W91YOpqjRy74vavhdUBEqcpUZZr/U/wKDsoKVRKt2BQ/zaKZ1MCKYELec gw9Es7nb8l0ngfy81M9xhN4M3fVWfnQTNLYuPQgrse78XfYgIGqLRNo6q w==; X-IronPort-AV: E=McAfee;i="6500,9779,10615"; a="394600702" X-IronPort-AV: E=Sophos;i="5.97,281,1669104000"; d="scan'208";a="394600702" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Feb 2023 20:16:48 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10615"; a="912982084" X-IronPort-AV: E=Sophos;i="5.97,281,1669104000"; d="scan'208";a="912982084" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga006.fm.intel.com with ESMTP; 08 Feb 2023 20:16:45 -0800 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.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, baolu.lu@linux.intel.com Subject: [PATCH 1/6] iommu: Add new iommu op to get iommu hardware information Date: Wed, 8 Feb 2023 20:16:37 -0800 Message-Id: <20230209041642.9346-2-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230209041642.9346-1-yi.l.liu@intel.com> References: <20230209041642.9346-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 autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757326056262525809?= X-GMAIL-MSGID: =?utf-8?q?1757326056262525809?= From: Lu Baolu Introduce a new iommu op get the IOMMU hardware capabilities for iommufd. This information will be used by any vIOMMU driver which is owned by userspace. 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. No warpper API is added as it is supposed to be used by iommufd only. Signed-off-by: Lu Baolu Signed-off-by: Yi Liu Signed-off-by: Nicolin Chen --- include/linux/iommu.h | 8 ++++++++ include/uapi/linux/iommufd.h | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/include/linux/iommu.h b/include/linux/iommu.h index cb586d054c57..97b398d19fd2 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -15,6 +15,7 @@ #include #include #include +#include #define IOMMU_READ (1 << 0) #define IOMMU_WRITE (1 << 1) @@ -223,6 +224,11 @@ struct iommu_iotlb_gather { /** * struct iommu_ops - iommu ops and capabilities * @capable: check capability + * @hw_info: IOMMU hardware capabilities. The type of the returned data is + * defined in include/uapi/linux/iommufd.h. The data buffer is + * allocated in the IOMMU driver and the caller should free it + * after use. Return the data buffer if success, or ERR_PTR on + * failure. * @domain_alloc: allocate iommu domain * @probe_device: Add device to iommu driver handling * @release_device: Remove device from iommu driver handling @@ -252,6 +258,7 @@ struct iommu_iotlb_gather { */ struct iommu_ops { bool (*capable)(struct device *dev, enum iommu_cap); + void *(*hw_info)(struct device *dev, u32 *length); /* Domain allocation and freeing by the iommu driver */ struct iommu_domain *(*domain_alloc)(unsigned iommu_domain_type); @@ -280,6 +287,7 @@ struct iommu_ops { void (*remove_dev_pasid)(struct device *dev, ioasid_t pasid); const struct iommu_domain_ops *default_domain_ops; + enum iommu_device_data_type driver_type; unsigned long pgsize_bitmap; struct module *owner; }; diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index 98ebba80cfa1..2309edb55028 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -344,4 +344,10 @@ struct iommu_vfio_ioas { __u16 __reserved; }; #define IOMMU_VFIO_IOAS _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VFIO_IOAS) + +/** + * enum iommu_device_data_type - IOMMU hardware Data types + */ +enum iommu_device_data_type { +}; #endif From patchwork Thu Feb 9 04:16:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 54704 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp119288wrn; Wed, 8 Feb 2023 20:25:36 -0800 (PST) X-Google-Smtp-Source: AK7set8IBWVwR/XYVVU9D8q35JzH6L/LQiGCIWis0qzuexBQIG7+DJA4RdX8soa4SKtapG7Htp/l X-Received: by 2002:a50:ab01:0:b0:4a2:5f73:d3d2 with SMTP id s1-20020a50ab01000000b004a25f73d3d2mr9859498edc.41.1675916735982; Wed, 08 Feb 2023 20:25:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675916735; cv=none; d=google.com; s=arc-20160816; b=hIb046LLLsiezqJY2ooX0YJp10b+UXqiJwZ9iAjDHh286r7vSr8W4ddoGwP3MOFqkW xFhsKucgtTLY3DRxabZD4UxLtz9OtC0BUUHow1Qboc+ttV41HRUyTApB3q1jSBoM7b8n Pt3y5dd+7Py9YEjnM6vP+6EDhagUHlXDGWnSD31fvJMDLFCo5XltfU2bBYgdChzitA7O hvBlJ2eKe1wOMO0AN1J+y9xHcDeskIkPbjTlIiXT1rOgM67JF6Gv1ZoHHop776zq58Ja kJhOgIma7XNzsrvkc4lFBbWgC/vbjg9WQxNckVvAr3ZbfMJJQkQpz0zbmou8FGiMolN7 E0Rg== 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=QOo/HngJRIVy2pWqDbQuUwKA40SiUx6Gd9BXdqzc6Tk=; b=TY7kip1dOnRFmSc/PM7FNHw1lVZObNH73tdCMRUvUJD5eM+JKiyROk5E+C3WxFI7QN 3gySgsbUoHFYotaq20eZ27rigZ8+xOVjPCbT3jMMeZc5chiguHCFgByZ1FdMfKdLx3XD Fun5Nejn5vz3Ch/MI69IRRGClSmUy3Y2OAKcF0EjeP61sRlo6H2ErXWmHHNQ/fMt/fdB hemwnnsIncxfBTxAkjzxFrwtCqLiHOxu5G+SlgcpB+PWU2x7ay2FoGNtOMt5aHoWJ93O n+QzpE8YJ3thA4encU9uq6G5TcMkB2I1wtdhLi7ZNrcNE9CbZwEKakmA5eN+0O/YXQzH DNFg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Ujdcylba; 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 c10-20020a056402100a00b004aac67f6f46si941688edu.17.2023.02.08.20.25.13; Wed, 08 Feb 2023 20:25:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Ujdcylba; 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 S229642AbjBIETw (ORCPT + 99 others); Wed, 8 Feb 2023 23:19:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33520 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229580AbjBIETs (ORCPT ); Wed, 8 Feb 2023 23:19:48 -0500 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4D7B837B46; Wed, 8 Feb 2023 20:18:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675916317; x=1707452317; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=FHpZ/QqiMDOUDlV/6MH/yoSElx9gu3oQTNg7c3bopYs=; b=UjdcylbahddBAdOaH8Fjk75LslKMVyuzY7+zGkxwiwr1n5d1JOYvxkCK 5nIm6Bc7GXVnv6c8ceYHyTonqExmCIDkWBXAu5joCeB28m1QdHOUKGRIA H+cuIdO00cC0vrwImfj/+BLE4BvctprfFQjklDohGZtdi4Oh9ymvgm4Bh /6B7aNUmjCpF7KEtUtp6G9Kz7NsvCpSFzBEI1Uv5iZU2CvurkaFQse43E GrGy4fQSjNPc9h5Cz5c6x1i9zpIFr6nyGwJLMxbIaXcEmoJO6GjB+fNUk KjO1JLXMNaH8Wajm7Hie8/csnRKl8r8qmf+QRXS4ta48vclESUGNPniPQ w==; X-IronPort-AV: E=McAfee;i="6500,9779,10615"; a="394600718" X-IronPort-AV: E=Sophos;i="5.97,281,1669104000"; d="scan'208";a="394600718" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Feb 2023 20:16:49 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10615"; a="912982088" X-IronPort-AV: E=Sophos;i="5.97,281,1669104000"; d="scan'208";a="912982088" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga006.fm.intel.com with ESMTP; 08 Feb 2023 20:16:47 -0800 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.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, baolu.lu@linux.intel.com Subject: [PATCH 2/6] iommu/vt-d: Implement hw_info for iommu capability query Date: Wed, 8 Feb 2023 20:16:38 -0800 Message-Id: <20230209041642.9346-3-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230209041642.9346-1-yi.l.liu@intel.com> References: <20230209041642.9346-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 autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757326067297061813?= X-GMAIL-MSGID: =?utf-8?q?1757326067297061813?= From: Lu Baolu To support nested translation in the userspace, it should check the underlying hardware information for the capabilities. Add intel_iommu_hw_info() to report cap_reg and ecap_reg information. Signed-off-by: Lu Baolu Signed-off-by: Yi Liu Signed-off-by: Nicolin Chen Signed-off-by: Yi Sun --- drivers/iommu/intel/iommu.c | 19 +++++++++++++++++++ drivers/iommu/intel/iommu.h | 1 + include/uapi/linux/iommufd.h | 21 +++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index 59df7e42fd53..929f600cc350 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -4760,8 +4760,26 @@ static void intel_iommu_remove_dev_pasid(struct device *dev, ioasid_t pasid) intel_pasid_tear_down_entry(iommu, dev, pasid, false); } +static void *intel_iommu_hw_info(struct device *dev, u32 *length) +{ + struct device_domain_info *info = dev_iommu_priv_get(dev); + struct intel_iommu *iommu = info->iommu; + struct iommu_device_info_vtd *vtd; + + vtd = kzalloc(sizeof(*vtd), GFP_KERNEL); + if (!vtd) + return ERR_PTR(-ENOMEM); + + vtd->cap_reg = iommu->cap; + vtd->ecap_reg = iommu->ecap; + *length = sizeof(*vtd); + + return vtd; +} + const struct iommu_ops intel_iommu_ops = { .capable = intel_iommu_capable, + .hw_info = intel_iommu_hw_info, .domain_alloc = intel_iommu_domain_alloc, .probe_device = intel_iommu_probe_device, .probe_finalize = intel_iommu_probe_finalize, @@ -4774,6 +4792,7 @@ const struct iommu_ops intel_iommu_ops = { .def_domain_type = device_def_domain_type, .remove_dev_pasid = intel_iommu_remove_dev_pasid, .pgsize_bitmap = SZ_4K, + .driver_type = IOMMU_DEVICE_DATA_INTEL_VTD, #ifdef CONFIG_INTEL_IOMMU_SVM .page_response = intel_svm_page_response, #endif diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h index 06e61e474856..2e70265d4ceb 100644 --- a/drivers/iommu/intel/iommu.h +++ b/drivers/iommu/intel/iommu.h @@ -22,6 +22,7 @@ #include #include #include +#include #include #include diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index 2309edb55028..fda75c8450ee 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -347,7 +347,28 @@ struct iommu_vfio_ioas { /** * enum iommu_device_data_type - IOMMU hardware Data types + * @IOMMU_DEVICE_DATA_INTEL_VTD: Intel VT-d iommu data type */ enum iommu_device_data_type { + IOMMU_DEVICE_DATA_INTEL_VTD = 1, +}; + +/** + * struct iommu_device_info_vtd - Intel VT-d device info + * + * @flags: Must be set to 0 + * @__reserved: Must be 0 + * @cap_reg: Value of Intel VT-d capability register defined in chapter + * 11.4.2 of Intel VT-d spec. + * @ecap_reg: Value of Intel VT-d capability register defined in chapter + * 11.4.3 of Intel VT-d spec. + * + * Intel hardware iommu capability. + */ +struct iommu_device_info_vtd { + __u32 flags; + __u32 __reserved; + __aligned_u64 cap_reg; + __aligned_u64 ecap_reg; }; #endif From patchwork Thu Feb 9 04:16:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 54705 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp119338wrn; Wed, 8 Feb 2023 20:25:45 -0800 (PST) X-Google-Smtp-Source: AK7set/bNQ1QrTyRw8+VgR+702FrxDW2VgZ/2EDupp/thLHDq9LjvxhRGlIuw+khilCpqG79dF0u X-Received: by 2002:a17:907:6da8:b0:8aa:c038:c768 with SMTP id sb40-20020a1709076da800b008aac038c768mr8653872ejc.66.1675916745185; Wed, 08 Feb 2023 20:25:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675916745; cv=none; d=google.com; s=arc-20160816; b=JPipsJmcd/MY5ZZ19V1tCgHNQD95SWooMPKqYe63ZmYVlo3muM6VM1egFtXDa2nEoe stiVI7jhYZrco3ppLc6FQJwQm+De0sIDXEH0MkzzT672IFfz6u082EVDOh/5KyC3KwzM 44tQ2QDxUBmyFWCKeGx9xC/jdA8Rr6hnzG/dV4aJCCEIJYqnzxq3McgkZx1/QNzGU0My ma4FA8u/KFCrGvTgt03O+mC1jdBkaUJ2lrC9hX+R1tvZyGEiWwVb2yKAO/7SB8JAkGBw nwRCp7uo+cs21DaXO/TnZfbK1hkHINa3zi+CxchfnDA7E2p5PMb7UuRjtztczBdGxst1 V2jA== 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=/5lJdCekBDGbrjfmDshPoMI8NaL1HhvLQrp48iaZVE8=; b=oi5X60p/peHe4TZOHPSYMFTmTyb/f+MHQGym2eoCQ73OIoFpazuI7NzJ4wAX9zK4m3 zcGkHsgvzRY7MyJz4mY0nOlyMXDeLuYhsDANOqHt1bxB/aCw0g+fzDKElGra3QZ9580n EKjYh/3uWyX+/u2anpm+WWzb24VOgOe6VN5ad0T0ieOvYFzsYUlMrq8fIFADTyzS/C+P 55DwT8HG665pb1CAO+YrSkWncT9q8tc3cghdXzJ4A6Ilz2BYfEHxvpeTm1AM1q5fhi2A iMxT7Tv6YGMwojiKVrT1CBw8JXG31rGShD/VYyE1uReD05m5AvUd1wiqGFvnn3JyC4yA ZyYw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=WZXVMdpl; 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 fd4-20020a1709072a0400b008af3b022a8dsi586050ejc.449.2023.02.08.20.25.21; Wed, 08 Feb 2023 20:25:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=WZXVMdpl; 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 S229625AbjBIEUV (ORCPT + 99 others); Wed, 8 Feb 2023 23:20:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33812 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229564AbjBIEUO (ORCPT ); Wed, 8 Feb 2023 23:20:14 -0500 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D86A0125AE; Wed, 8 Feb 2023 20:19:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675916360; x=1707452360; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Zg75NlnzG6bl+FEokzV5mm4wJdtzxfEPl+1gdcoX9v4=; b=WZXVMdplkhYeXlnDg6B7TelrovySZiy+APS08/1iBteCNqI41nyFzYKj dyCTzInlDBZbK4qr2Lp3z6kdObpT+tu6tkEiOMiQwhbmv7F3MHsmNmfnt 88mcbJLZR2PGR/8TULYEBJ91K9vDLXf5sVzdxjZfl0RXrx5ZEEihRK2l+ eFN4AgREXOvJ2XdQCzrW1JkzkKZf10RZJu6nELnrRjlk8xdbh4Tr0b2y/ y5fl0b4ANRE09JTp41uHjLjOTlji493IiGx99hyCJOCj1Vcfxi2OXcAC5 CLq78y2smkZdwACzfk9DcxZF2w0meBYnXIBTWNx+NiLkVcP10nGhwfuk0 Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10615"; a="394600728" X-IronPort-AV: E=Sophos;i="5.97,281,1669104000"; d="scan'208";a="394600728" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Feb 2023 20:16:51 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10615"; a="912982101" X-IronPort-AV: E=Sophos;i="5.97,281,1669104000"; d="scan'208";a="912982101" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga006.fm.intel.com with ESMTP; 08 Feb 2023 20:16:49 -0800 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.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, baolu.lu@linux.intel.com Subject: [PATCH 3/6] iommufd: Add IOMMU_DEVICE_GET_INFO Date: Wed, 8 Feb 2023 20:16:39 -0800 Message-Id: <20230209041642.9346-4-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230209041642.9346-1-yi.l.liu@intel.com> References: <20230209041642.9346-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 autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757326076980519894?= X-GMAIL-MSGID: =?utf-8?q?1757326076980519894?= Under nested IOMMU translation, userspace owns the stage-1 translation structure (e.g. the stage-1 page table of Intel VT-d or the context table of ARM SMMUv3, and etc.). Such stage-1 translation structures are vendor specific, and needs to be compatiable with the underlying IOMMU hardware. Hence, userspace should know the IOMMU hardware capability before creating and configuring the stage-1 translation structure to kernel. This adds ioctl: IOMMU_DEVICE_GET_INFO to query the IOMMU hardware capability for a given device. The returned data is vendor specific, userspace can tell it by the @out_device_type field. Signed-off-by: Nicolin Chen Signed-off-by: Yi Liu --- drivers/iommu/iommufd/device.c | 72 +++++++++++++++++++++++++ drivers/iommu/iommufd/iommufd_private.h | 1 + drivers/iommu/iommufd/main.c | 3 ++ include/uapi/linux/iommufd.h | 36 +++++++++++++ 4 files changed, 112 insertions(+) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 8a9834fc129a..3b64aef24807 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -134,6 +134,78 @@ void iommufd_device_unbind(struct iommufd_device *idev) } EXPORT_SYMBOL_NS_GPL(iommufd_device_unbind, IOMMUFD); +static int iommufd_zero_fill_user(u64 ptr, int bytes) +{ + int index = 0; + + for (; index < bytes; index++) { + if (put_user(0, (uint8_t __user *)(ptr + index))) + return -EFAULT; + } + return 0; +} + +int iommufd_device_get_info(struct iommufd_ucmd *ucmd) +{ + struct iommu_device_info *cmd = ucmd->cmd; + struct iommufd_object *dev_obj; + struct device *dev; + const struct iommu_ops *ops; + void *data; + unsigned int length, data_len; + int rc; + + if (cmd->flags || cmd->__reserved || !cmd->data_len) + return -EOPNOTSUPP; + + dev_obj = iommufd_get_object(ucmd->ictx, cmd->dev_id, + IOMMUFD_OBJ_DEVICE); + if (IS_ERR(dev_obj)) + return PTR_ERR(dev_obj); + + dev = container_of(dev_obj, struct iommufd_device, obj)->dev; + + ops = dev_iommu_ops(dev); + if (!ops || !ops->hw_info) { + rc = -EOPNOTSUPP; + goto out_put; + } + + data = ops->hw_info(dev, &data_len); + if (IS_ERR(data)) { + rc = PTR_ERR(data); + goto out_put; + } + + length = min(cmd->data_len, data_len); + if (copy_to_user(u64_to_user_ptr(cmd->data_ptr), data, length)) { + rc = -EFAULT; + goto out_free_data; + } + + /* + * Zero the trailing bytes for userspace if the buffer is bigger + * than the data size kernel actually has. + */ + if (length < cmd->data_len) { + rc = iommufd_zero_fill_user(cmd->data_ptr + length, + cmd->data_len - length); + if (rc) + goto out_free_data; + } + + cmd->out_device_type = ops->driver_type; + cmd->data_len = data_len; + + rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd)); + +out_free_data: + kfree(data); +out_put: + iommufd_put_object(dev_obj); + return rc; +} + static int iommufd_device_setup_msi(struct iommufd_device *idev, struct iommufd_hw_pagetable *hwpt, phys_addr_t sw_msi_start) diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 200c783800ad..4a0a1a7fdae1 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -258,6 +258,7 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, void iommufd_hw_pagetable_destroy(struct iommufd_object *obj); void iommufd_device_destroy(struct iommufd_object *obj); +int iommufd_device_get_info(struct iommufd_ucmd *ucmd); struct iommufd_access { struct iommufd_object obj; diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index 3fbe636c3d8a..59aa30ad1090 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -250,6 +250,7 @@ static int iommufd_option(struct iommufd_ucmd *ucmd) union ucmd_buffer { struct iommu_destroy destroy; + struct iommu_device_info info; struct iommu_ioas_alloc alloc; struct iommu_ioas_allow_iovas allow_iovas; struct iommu_ioas_copy ioas_copy; @@ -281,6 +282,8 @@ struct iommufd_ioctl_op { } static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = { IOCTL_OP(IOMMU_DESTROY, iommufd_destroy, struct iommu_destroy, id), + IOCTL_OP(IOMMU_DEVICE_GET_INFO, iommufd_device_get_info, struct iommu_device_info, + __reserved), 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 fda75c8450ee..6cfe102f26f3 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -45,6 +45,7 @@ enum { IOMMUFD_CMD_IOAS_UNMAP, IOMMUFD_CMD_OPTION, IOMMUFD_CMD_VFIO_IOAS, + IOMMUFD_CMD_DEVICE_GET_INFO, }; /** @@ -371,4 +372,39 @@ struct iommu_device_info_vtd { __aligned_u64 cap_reg; __aligned_u64 ecap_reg; }; + +/** + * struct iommu_device_info - ioctl(IOMMU_DEVICE_GET_INFO) + * @size: sizeof(struct iommu_device_info) + * @flags: Must be 0 + * @dev_id: The device being attached to the IOMMU + * @data_len: Input the type specific data buffer length in bytes + * @data_ptr: Pointer to the type specific structure (e.g. + * struct iommu_device_info_vtd) + * @out_device_type: Output the underlying iommu hardware type, it is + * one of enum iommu_device_data_type. + * @__reserved: Must be 0 + * + * Query the hardware iommu capability for given device which has been + * bound to iommufd. @data_len is set to be the size of the buffer to + * type specific data and the data will be filled. Trailing bytes are + * zeroed if the user buffer is larger than the data kernel has. + * + * The type specific data would be used to sync capability between the + * vIOMMU and the hardware IOMMU, also for the availabillity checking of + * iommu hardware features like dirty page tracking in I/O page table. + * + * The @out_device_type will be filled if the ioctl succeeds. It would + * be used to decode the data filled in the buffer pointed by @data_ptr. + */ +struct iommu_device_info { + __u32 size; + __u32 flags; + __u32 dev_id; + __u32 data_len; + __aligned_u64 data_ptr; + __u32 out_device_type; + __u32 __reserved; +}; +#define IOMMU_DEVICE_GET_INFO _IO(IOMMUFD_TYPE, IOMMUFD_CMD_DEVICE_GET_INFO) #endif From patchwork Thu Feb 9 04:16:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 54706 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp119396wrn; Wed, 8 Feb 2023 20:25:56 -0800 (PST) X-Google-Smtp-Source: AK7set/Zuq8uZmYAVBON7OlXBbr96T4Y98btLrru8XPWpcSrqCnrRZw48X1zGs0KMODL4l4mfcGy X-Received: by 2002:a17:906:1399:b0:7c0:be5d:59a9 with SMTP id f25-20020a170906139900b007c0be5d59a9mr5799061ejc.20.1675916756452; Wed, 08 Feb 2023 20:25:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675916756; cv=none; d=google.com; s=arc-20160816; b=tn6mtQjT/lZWJ5RgrAMCCRv5x8aG1f/LCcIPqPPV7gnPpuA5bx7+ppZnHlFKizJx5Z IwbtoOSx7a6765sz1G/h9GhvdYdfQ+sBJ3t+Zw4tPxnnAnWnYkftuglt8OVa/kBBqE6H n1sPxbRyLg7bBqogjo3syszw2nKdZK58oNbrV5lk2Joipq7vjcbwM2yyEqGPyfZTzFRw AGYeBr944tv/eQ1clAg6z+doENhK8RhEFgxpj17gUewYkanoSRpF6j5IH7FZuMocDs8Y AieCvlzIv19eOkBKOxzcb/Rh6EtDSCjRtnYohikzF3npANxiXro67WdVwE9no/V/40vV uidA== 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=3NfEVCYE6BSl+zC+ajO3dteEkARi4C5fblSa4/8Gh38=; b=Tdj2GIMXbgQe/afue6fnP92otuF8/pjr95T5R/cSOnHF3R+0vrIJhnkU3DOIkAcE68 xzUhI4lOvs0qUb5D4RugFXucPPN46Qpn10j62yCeRhpLSa2+jAu1GHFog88PjG7kiqKe /+1LsPfc0gyrwwZtvsYX9aPr/1qyEsz8mjKmS2TWOrparTCxO/8h0O0AUUMnHmPea8s7 4qhttKZBMDnhqDQVw4owdnRlZ+oLVAPfYioLKqE2qEmnUr6m/4u8EUE5JrtPJPgC0iiZ 9RYnzaGyWb1aNcQnp8VisCGh+KPq5hDkW5l2ASNUsiho7ewn18b22Y0luSZG8b6rXPyV uCKg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=DuT36Fuy; 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 fi22-20020a1709073ad600b0088e18383de8si991765ejc.613.2023.02.08.20.25.33; Wed, 08 Feb 2023 20:25:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=DuT36Fuy; 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 S229632AbjBIEUY (ORCPT + 99 others); Wed, 8 Feb 2023 23:20:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33882 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229602AbjBIEUT (ORCPT ); Wed, 8 Feb 2023 23:20:19 -0500 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 177085BB6; Wed, 8 Feb 2023 20:19:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675916364; x=1707452364; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=FvzP1cm3MJo8mMYBDAYm1g80hB7lsWqyw4EEdmHNXb4=; b=DuT36FuySU9sAa4zuVmP34locoWLSwXQu3814a2c8s21lGEvF4ASJQpz 9Ou73zbYJ/65EwgBZSPGWHWYUwfGFCzTaO11XjpitqTYhOC0tBdIcO+LY soyEwGLNuEC2AJBWcRjlj/g7jhtO5SidoVqYtWDZVaAL/k98bTsxcb2aB J03EoWYizV3nhUGMnh2ECamyiCm8x0AM4wSjnYvTk5t34LbRMJD2qXDQC ElvSk4ImnaWJAtWjFhXrjwnabZdIz0Q/rLVQvGjnB7bFEra227nUcvS3v 6MLSeyqnX2imXYFhU4G128/wN/ohZ8c30wVXGvx0iwgWrRCBMiPA11bMq w==; X-IronPort-AV: E=McAfee;i="6500,9779,10615"; a="394600742" X-IronPort-AV: E=Sophos;i="5.97,281,1669104000"; d="scan'208";a="394600742" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Feb 2023 20:16:53 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10615"; a="912982117" X-IronPort-AV: E=Sophos;i="5.97,281,1669104000"; d="scan'208";a="912982117" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga006.fm.intel.com with ESMTP; 08 Feb 2023 20:16:51 -0800 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.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, baolu.lu@linux.intel.com Subject: [PATCH 4/6] iommufd/device: Add mock_device support in iommufd_device_get_info() Date: Wed, 8 Feb 2023 20:16:40 -0800 Message-Id: <20230209041642.9346-5-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230209041642.9346-1-yi.l.liu@intel.com> References: <20230209041642.9346-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 autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757326088484665783?= X-GMAIL-MSGID: =?utf-8?q?1757326088484665783?= This prepares for adding selftest for IOMMU_DEVICE_GET_INFO. Selftest uses mock device, while physical device uses iommufd_device, so add a helper iommufd_obj_dev() to get struct device from the iommufd_object for a given dev_id. Signed-off-by: Nicolin Chen Signed-off-by: Yi Liu --- drivers/iommu/iommufd/device.c | 22 ++++++++++++++++++++-- drivers/iommu/iommufd/iommufd_private.h | 1 + drivers/iommu/iommufd/selftest.c | 7 +++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 3b64aef24807..470838e6902d 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -145,6 +145,20 @@ static int iommufd_zero_fill_user(u64 ptr, int bytes) return 0; } +static struct device * +iommufd_obj_dev(struct iommufd_object *obj) +{ + struct device *dev = NULL; + + if (obj->type == IOMMUFD_OBJ_DEVICE) + dev = container_of(obj, struct iommufd_device, obj)->dev; +#ifdef CONFIG_IOMMUFD_TEST + else if (obj->type == IOMMUFD_OBJ_SELFTEST) + dev = iommufd_selftest_obj_to_dev(obj); +#endif + return dev; +} + int iommufd_device_get_info(struct iommufd_ucmd *ucmd) { struct iommu_device_info *cmd = ucmd->cmd; @@ -159,11 +173,15 @@ int iommufd_device_get_info(struct iommufd_ucmd *ucmd) return -EOPNOTSUPP; dev_obj = iommufd_get_object(ucmd->ictx, cmd->dev_id, - IOMMUFD_OBJ_DEVICE); + IOMMUFD_OBJ_ANY); if (IS_ERR(dev_obj)) return PTR_ERR(dev_obj); - dev = container_of(dev_obj, struct iommufd_device, obj)->dev; + dev = iommufd_obj_dev(dev_obj); + if (!dev) { + rc = -EINVAL; + goto out_put; + } ops = dev_iommu_ops(dev); if (!ops || !ops->hw_info) { diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 4a0a1a7fdae1..7748117e36f9 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -284,6 +284,7 @@ iommufd_device_selftest_attach(struct iommufd_ctx *ictx, struct device *mock_dev); void iommufd_device_selftest_detach(struct iommufd_ctx *ictx, struct iommufd_hw_pagetable *hwpt); +struct device *iommufd_selftest_obj_to_dev(struct iommufd_object *obj); int iommufd_test(struct iommufd_ucmd *ucmd); void iommufd_selftest_destroy(struct iommufd_object *obj); extern size_t iommufd_test_memory_limit; diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c index b94870f93138..2ecde22a60f4 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -802,6 +802,13 @@ void iommufd_selftest_destroy(struct iommufd_object *obj) } } +struct device *iommufd_selftest_obj_to_dev(struct iommufd_object *obj) +{ + struct selftest_obj *sobj = container_of(obj, struct selftest_obj, obj); + + return &sobj->idev.mock_dev; +} + int iommufd_test(struct iommufd_ucmd *ucmd) { struct iommu_test_cmd *cmd = ucmd->cmd; From patchwork Thu Feb 9 04:16:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 54707 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp119410wrn; Wed, 8 Feb 2023 20:25:59 -0800 (PST) X-Google-Smtp-Source: AK7set+NTHx0uvOdVLB8RdOn1bhlQCFj31/Ce6ynyvVeiN2mNZ9jy80/4bHwt3+z7SZWLexMSE0j X-Received: by 2002:a50:d7d9:0:b0:49d:1c3a:b5fb with SMTP id m25-20020a50d7d9000000b0049d1c3ab5fbmr11204072edj.28.1675916758857; Wed, 08 Feb 2023 20:25:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675916758; cv=none; d=google.com; s=arc-20160816; b=j8hXJZWlEhwE4OJLFIXtL/gsY+EuhIFw0pMgqQL5QCB1V/GXV8RWy2rzbW9Vt++vU5 V9aPWiFZylQnh+4gkRGonk3cWfb7EZyGQ+QJHfuPuZFh6Si3ORmqMsao5fjRrlBhjl7G ugUDt/Bo2sN0Z/9VTeyUFfqAWM5eu82cC9aqcS4sB014b79HpoARw/P1IMOBhhIJTwI2 5nnr/MqPHxfL3mQHlaZc3bnue2FoH4RIaN0hdWEQKzjUs7Cum7kRzxQn0J4lPMfwxoH2 cpfi+07Pjvsq30LRXaSeprU+DOUm6mJhyLbEtIUn2HAMDpMWAVg3MJIbN0ZwBp7kZZVd 6u/w== 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=yE5vxZBNfrMuPuCYsBDjhOhWR5gdELGOoInYTq0+u1M=; b=aHWdk3VPtXN25nWK84aTiC4caMfW3GYQgUUWuxMpA9+BlPYCmrZlgkRrMN6NufD+ua RVqynQYuULW4Irjj03V4fgr+ZxQWNf4kxlzP7pMjGE4U/FNP6pbJX/YgczS1V7U20Ccj /fnqvDlaAImYXDg4tTrkwjFIS01VDI0l2+CKlppHz2BQn/NYvDqxfBR2ypW5vuRpONXX +KJ3EZrl8zPWBULaUrLJMNSS2QE93MaF75FqPDfebsau9t2WAIq3Rya77Jwd4iLkQHSr IM2wN2BIPVdfEZYbUEZY5MB88cXKSGldFJTlIYXGN0dBkqLKFVbjKAIyWN6s5RZsBfdt 4tig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=l+ZwhvxA; 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 k14-20020aa7c38e000000b004aab4c0a185si927107edq.51.2023.02.08.20.25.36; Wed, 08 Feb 2023 20:25:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=l+ZwhvxA; 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 S229653AbjBIEU2 (ORCPT + 99 others); Wed, 8 Feb 2023 23:20:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33898 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229629AbjBIEUW (ORCPT ); Wed, 8 Feb 2023 23:20:22 -0500 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A46E91A95D; Wed, 8 Feb 2023 20:19:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675916367; x=1707452367; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=HKkQ3XLh/cgddbfTjAzae85+rSiZHI2gRNi0xQo2hVQ=; b=l+ZwhvxAJzdUjkfPoz4EG/G7HBdWBqT8G4Duaa3ifPIjNu2NaeJuTRzz bquZ49LBbZCauWhiIbJqfWipQl9qyLrrE9STsoDd6qmSEw4IorsJrcUwz K85faZHY7thNoifj/illtyjKyxOP6ZpAOHt9KapgbpCqtfzkaR76YGVMt SqjXfURM5FlA1KYRxAcfnWfZs/11cJnVUwiov2BKm/0tq226+JCOTBoTN WG33CuZnKrZXgQbjD7yaIpBX8L3WYC7VgPkhAvmJmaMgbPzv0laytLGCT 4n2KieltwojgQJXFXYKMwaO46VlE4gDcxroXKA18NvmwkEY4JNK4trX8+ Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10615"; a="394600757" X-IronPort-AV: E=Sophos;i="5.97,281,1669104000"; d="scan'208";a="394600757" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Feb 2023 20:16:56 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10615"; a="912982130" X-IronPort-AV: E=Sophos;i="5.97,281,1669104000"; d="scan'208";a="912982130" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga006.fm.intel.com with ESMTP; 08 Feb 2023 20:16:54 -0800 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.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, baolu.lu@linux.intel.com Subject: [PATCH 5/6] iommufd/selftest: Set iommu_device for mock_device Date: Wed, 8 Feb 2023 20:16:41 -0800 Message-Id: <20230209041642.9346-6-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230209041642.9346-1-yi.l.liu@intel.com> References: <20230209041642.9346-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 autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757326091034336193?= X-GMAIL-MSGID: =?utf-8?q?1757326091034336193?= From: Nicolin Chen This is required to avoid NULL pointer when iommufd selftest tries to allocate domain for mock_device. Signed-off-by: Nicolin Chen Signed-off-by: Yi Liu --- drivers/iommu/iommufd/selftest.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c index 2ecde22a60f4..5afed3fc30fe 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -273,6 +273,8 @@ get_md_pagetable(struct iommufd_ucmd *ucmd, u32 mockpt_id, static int iommufd_test_mock_domain(struct iommufd_ucmd *ucmd, struct iommu_test_cmd *cmd) { + static struct iommu_device iommu_dev = { .ops = &mock_ops }; + static struct dev_iommu iommu = { .iommu_dev = &iommu_dev }; static struct bus_type mock_bus = { .iommu_ops = &mock_ops }; struct iommufd_hw_pagetable *hwpt; struct selftest_obj *sobj; @@ -291,6 +293,7 @@ static int iommufd_test_mock_domain(struct iommufd_ucmd *ucmd, sobj->idev.ictx = ucmd->ictx; sobj->type = TYPE_IDEV; sobj->idev.mock_dev.bus = &mock_bus; + sobj->idev.mock_dev.iommu = &iommu; hwpt = iommufd_device_selftest_attach(ucmd->ictx, ioas, &sobj->idev.mock_dev); From patchwork Thu Feb 9 04:16:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 54708 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp119488wrn; Wed, 8 Feb 2023 20:26:12 -0800 (PST) X-Google-Smtp-Source: AK7set918h4CYAaDDuHrL+awQ2T/r6gnjR2n+jona/zBgK2WemJHyH91iz3iC4N+XVY5HuuoyRLG X-Received: by 2002:a17:906:36d9:b0:878:7921:9a94 with SMTP id b25-20020a17090636d900b0087879219a94mr9272282ejc.16.1675916772793; Wed, 08 Feb 2023 20:26:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675916772; cv=none; d=google.com; s=arc-20160816; b=NhcaRMudCEXuZPrfBsvdavdnJQCIpERXfAumZp/FB0kpDLfScUZpUf/WYNQsKUAY5Y mSXdm54/KaUOTcF0VRanl8lCKtMh01avi7yNHXyJ54ge20eWhaccf4u0E06P+uZhpLyk RY/ekCnwk5S4T9r67bJEiwD9tIbzQjvNZ9hzrnPTzxlCoMrDFnbfzQA1z0jhvZ7FUGh5 2p2vCyH0qE3KioDKAouCnrX1iqQtg1t00ltCJe0+BK7+/aNyWjFZqlnsO9Y7LAj3rOpJ 6KEETxWwMYh3/HvmOFGUFV5ZiIA7PBIVJdgsjEOou4euMxkCdDDVBrwMRTUQ+GEVZEcB hNzQ== 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=uLUeMYwKPZRaBO73aj0mIZgdk4/yg2k90/ans275QfU=; b=MqQQDD9h9jTHa/ISgNVMG/bvVNmFI/2lTBiHp1lDPXMVcHmMz0HWeyeBlZ7o6yP1+6 pelM2Nl4443X/f37Ldm2JhxFMQt82d3Qdla13r+kufVmpJ18uSZHe20Va2yp4RI9QVMx yuy87Pky4KzoeGV8LQGxMskVSdrsI0vhfFbARgIFYdOP9fUqSZ/72jeNI5FUpXbg+Qug zAK1e3pbF+wbDKZx8OJuu8/sY9n/wE7cBNt8R2BKZjWGR8xjc2uXYVLe+D1kflfvgs/S CoE5Yluv0alSPkYPf70Gn8WUEaOtDbZx6bOPN8DR73ra87A4khZu2x4oIaC/AO/zrDIn 0QTQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ixHXGSQ9; 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 17-20020a170906019100b0089f801f775fsi1430352ejb.694.2023.02.08.20.25.49; Wed, 08 Feb 2023 20:26:12 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ixHXGSQ9; 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 S229675AbjBIEVN (ORCPT + 99 others); Wed, 8 Feb 2023 23:21:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34484 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229460AbjBIEVK (ORCPT ); Wed, 8 Feb 2023 23:21:10 -0500 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4A861DBCD; Wed, 8 Feb 2023 20:20:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675916414; x=1707452414; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=vnj0cCi2ra/GTQprP6oGdeKLwYac70d/g6YVtvXPxLQ=; b=ixHXGSQ98xLDq0naL4+db3i/9Jcs86tfL/LTZu/Pc8vW4DVHPukE9WCa KK2fdLYwHMt7tNziWw8nCMYnxtJZX2omVnScKEETw/SjI44rrsOTn7JeS OYzNBwSyockfncxU33pRhEPkF8AgMVFtQ7pplvLMaCOxmB/l0+eOjRdME ZDzpU9BPos6LQW9OvilY92QYzqVebtT6Lx6AoSQZfg5gP8b0HjBitmZNV 6SRLuh+G7LvxiuSLbmJBWWyeBwzuHSZ9JvqOsgHFBJs/uDdNrQ+cpd95f bR0sx6R3bhX5pKWMEKkuorDPg6E9MT5mO2vXMKvETgxmt5UgheAPxFFce g==; X-IronPort-AV: E=McAfee;i="6500,9779,10615"; a="394600768" X-IronPort-AV: E=Sophos;i="5.97,281,1669104000"; d="scan'208";a="394600768" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Feb 2023 20:16:57 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10615"; a="912982142" X-IronPort-AV: E=Sophos;i="5.97,281,1669104000"; d="scan'208";a="912982142" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga006.fm.intel.com with ESMTP; 08 Feb 2023 20:16:56 -0800 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.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, baolu.lu@linux.intel.com Subject: [PATCH 6/6] iommufd/selftest: Add coverage for IOMMU_DEVICE_GET_INFO ioctl Date: Wed, 8 Feb 2023 20:16:42 -0800 Message-Id: <20230209041642.9346-7-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230209041642.9346-1-yi.l.liu@intel.com> References: <20230209041642.9346-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 autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757326105878641193?= X-GMAIL-MSGID: =?utf-8?q?1757326105878641193?= From: Nicolin Chen This allows to get info for the mock_dev, helps to run selftest to check IOMMU_DEVICE_GET_INFO in selftest. Signed-off-by: Nicolin Chen Signed-off-by: Yi Liu --- drivers/iommu/iommufd/device.c | 1 + drivers/iommu/iommufd/iommufd_test.h | 15 +++++++++++ drivers/iommu/iommufd/selftest.c | 16 ++++++++++++ tools/testing/selftests/iommu/iommufd.c | 18 ++++++++++++- tools/testing/selftests/iommu/iommufd_utils.h | 26 +++++++++++++++++++ 5 files changed, 75 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 470838e6902d..0e5d2bde7b3c 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -8,6 +8,7 @@ #include "io_pagetable.h" #include "iommufd_private.h" +#include "iommufd_test.h" MODULE_IMPORT_NS(IOMMUFD_INTERNAL); diff --git a/drivers/iommu/iommufd/iommufd_test.h b/drivers/iommu/iommufd/iommufd_test.h index f2c61a9500e7..1605ff2b1a90 100644 --- a/drivers/iommu/iommufd/iommufd_test.h +++ b/drivers/iommu/iommufd/iommufd_test.h @@ -94,4 +94,19 @@ struct iommu_test_cmd { }; #define IOMMU_TEST_CMD _IO(IOMMUFD_TYPE, IOMMUFD_CMD_BASE + 32) +/* Mock structs for IOMMU_DEVICE_GET_INFO ioctl */ +#define IOMMU_DEVICE_DATA_SELFTEST 0xfeedbeef +#define IOMMU_DEVICE_INFO_SELFTEST_REGVAL 0xdeadbeef + +/** + * struct iommu_device_info_selftest + * + * @flags: Must be set to 0 + * @test_reg: Pass IOMMU_DEVICE_INFO_SELFTEST_REGVAL to user selftest program + */ +struct iommu_device_info_selftest { + __u32 flags; + __u32 test_reg; +}; + #endif diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c index 5afed3fc30fe..5013c8757f4b 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -104,6 +104,20 @@ struct selftest_obj { }; }; +static void *mock_domain_hw_info(struct device *dev, u32 *length) +{ + struct iommu_device_info_selftest *info; + + info = kzalloc(sizeof(*info), GFP_KERNEL); + if (!info) + return ERR_PTR(-ENOMEM); + + info->test_reg = IOMMU_DEVICE_INFO_SELFTEST_REGVAL; + *length = sizeof(*info); + + return info; +} + static struct iommu_domain *mock_domain_alloc(unsigned int iommu_domain_type) { struct mock_iommu_domain *mock; @@ -239,6 +253,8 @@ static phys_addr_t mock_domain_iova_to_phys(struct iommu_domain *domain, static const struct iommu_ops mock_ops = { .owner = THIS_MODULE, .pgsize_bitmap = MOCK_IO_PAGE_SIZE, + .driver_type = IOMMU_DEVICE_DATA_SELFTEST, + .hw_info = mock_domain_hw_info, .domain_alloc = mock_domain_alloc, .default_domain_ops = &(struct iommu_domain_ops){ diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c index 1e293950ac88..8e1369451464 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -124,6 +124,7 @@ TEST_F(iommufd, cmd_length) TEST_LENGTH(iommu_ioas_unmap, IOMMU_IOAS_UNMAP); TEST_LENGTH(iommu_option, IOMMU_OPTION); TEST_LENGTH(iommu_vfio_ioas, IOMMU_VFIO_IOAS); + TEST_LENGTH(iommu_device_info, IOMMU_DEVICE_GET_INFO); #undef TEST_LENGTH } @@ -185,6 +186,7 @@ TEST_F(iommufd, global_options) FIXTURE(iommufd_ioas) { int fd; + uint32_t dev_id; uint32_t ioas_id; uint32_t domain_id; uint64_t base_iova; @@ -212,7 +214,8 @@ FIXTURE_SETUP(iommufd_ioas) } for (i = 0; i != variant->mock_domains; i++) { - test_cmd_mock_domain(self->ioas_id, NULL, &self->domain_id); + test_cmd_mock_domain(self->ioas_id, &self->dev_id, + &self->domain_id); self->base_iova = MOCK_APERTURE_START; } } @@ -281,6 +284,19 @@ TEST_F(iommufd_ioas, ioas_area_auto_destroy) } } +TEST_F(iommufd_ioas, device_get_info) +{ + struct iommu_device_info_selftest info; + + if (self->dev_id) { + test_cmd_device_get_info(self->dev_id, sizeof(info), &info); + assert(info.test_reg == IOMMU_DEVICE_INFO_SELFTEST_REGVAL); + } else { + test_err_device_get_info(ENOENT, self->dev_id, + sizeof(info), &info); + } +} + TEST_F(iommufd_ioas, area) { int i; diff --git a/tools/testing/selftests/iommu/iommufd_utils.h b/tools/testing/selftests/iommu/iommufd_utils.h index 67805afc620f..1807d29c05f8 100644 --- a/tools/testing/selftests/iommu/iommufd_utils.h +++ b/tools/testing/selftests/iommu/iommufd_utils.h @@ -294,3 +294,29 @@ static void teardown_iommufd(int fd, struct __test_metadata *_metadata) }) #endif + +static int _test_cmd_device_get_info(int fd, __u32 device_id, + __u32 data_len, void *data) +{ + struct iommu_device_info cmd = { + .size = sizeof(cmd), + .dev_id = device_id, + .data_len = data_len, + .data_ptr = (uint64_t)data, + }; + int ret; + + ret = ioctl(fd, IOMMU_DEVICE_GET_INFO, &cmd); + if (ret) + return ret; + return 0; +} + +#define test_cmd_device_get_info(device_id, data_len, data) \ + ASSERT_EQ(0, _test_cmd_device_get_info(self->fd, device_id, \ + data_len, data)) + +#define test_err_device_get_info(_errno, device_id, data_len, data) \ + EXPECT_ERRNO(_errno, \ + _test_cmd_device_get_info(self->fd, device_id, \ + data_len, data))