From patchwork Thu Jan 11 04:10:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 187157 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2411:b0:101:2151:f287 with SMTP id m17csp1218918dyi; Wed, 10 Jan 2024 20:11:06 -0800 (PST) X-Google-Smtp-Source: AGHT+IHnCdB5vIB+znm4n+2wTru1l3XIv+Mx4Ol+LEIcSGh/1zNjWtvLpkz0dOsRZY7sf7Lin2Yq X-Received: by 2002:a05:620a:1376:b0:783:1b7b:aa89 with SMTP id d22-20020a05620a137600b007831b7baa89mr782886qkl.79.1704946266495; Wed, 10 Jan 2024 20:11:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704946266; cv=none; d=google.com; s=arc-20160816; b=yyotH1iF4F7txE7DGIkv6JqDAqh/a1yAx1NW5oRSmdb2nOLCqqMCIAdMBh/WfjzDyp E/k7g2ElYELPmzMesivjZnm59abhL9+A1v+lTjGRnmHyNdXhRgvE9w4qVxletcTlp2Iq mRTrDXbabGW9pTJTjzhv+i8B3i2jmCLPkJy60YIYW3v97Eu9Liai14PnZG7/JKMmAg9a I3ERC1QLjmdUBI2Z2wZLoSG4YX/IdxfBlCmxvbCfqVDk7UCCmRktT+mSdlBBo+mva7Sc eMMLOMVGeTMZzngC8mUyk+vUN69T0d4UpPBmh2TYwqoHr1NFIwrcDaSlIWYm8B0JCWXL IP9Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=G4fmvkcaeOHHBHLQRAT2Jn/KP4KGND5PuhBsZcLHVxg=; fh=aa7ajXSXkJe0gRz4CDJDDWs+jFd1z8IECXwIf+VZeM8=; b=xV34QJPxCbqMpsXToIP3l2bSQ1K0BOzT7UIKTokqZxn2gylFkSvTZKFzbsUNHhvYpf emH72h2ww6eNbVpGBC/voqnT05gCP1/gZK8zmB8PNI4SS79STXe9MchvRdJzyYswzbQE Anwz9bUSadh7s7FRMeEOtsIo+8l55DpAuo2jm7cxGX2s8r+MK7NfzaelyqJcxcpb7zwn dmkIWa8BdMDjdZrOQpB89SBpq1ZK2YHsjyW+fvFrPcSzC5dPwDPq7gHQo0Z6yO/edBDJ DNlOWjcCTtW/eBc4Mx7ovmD6rdypz6sROC/bfFMtEGp42JQbubvNvIGIud7r0vm4ReYt zJ9w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=gB+PzIrH; spf=pass (google.com: domain of linux-kernel+bounces-23048-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-23048-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id u18-20020a05620a121200b007819059f7efsi189481qkj.766.2024.01.10.20.11.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 20:11:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-23048-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=gB+PzIrH; spf=pass (google.com: domain of linux-kernel+bounces-23048-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-23048-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 0CC551C22DEE for ; Thu, 11 Jan 2024 04:11:06 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 341D55672; Thu, 11 Jan 2024 04:10:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="gB+PzIrH" Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.20]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0867210F3; Thu, 11 Jan 2024 04:10:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1704946224; x=1736482224; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=eheKmIiZIGJEgkz55tUg5C8gYGgoEc13PJeJt4LC/O0=; b=gB+PzIrHkIHk7/12B7FxbgX5cSgS+ZcvaDOutlljYbbX4Qxo4hwKCwT+ 9x8HMPS0iNPU4ivEQFNfzocoKss/T1WkVYt7QNubOYA8XWg2hnXnUBRmx 4LizDIAGqudwQeJRfqc3zm5a3zqQV7TXFs6nTDTHOGrxYle9I5OJXR6S0 1O1r+atlsgN6VCMkCJ0wm1OYMgDyhW1K02Ol9kr50wTG9N/08qdlM4RQD v8ZFTMyAAyeYmucD8lRTuPncoYh83zooe1AA416P5kaTXbnGRt0w8nBcm BTUotg2nqsSipZ3ZvsN9bSVQEnW9BW3Ah0qMCk3tk0nxk60VwoN+EZkn5 g==; X-IronPort-AV: E=McAfee;i="6600,9927,10949"; a="389167612" X-IronPort-AV: E=Sophos;i="6.04,185,1695711600"; d="scan'208";a="389167612" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jan 2024 20:10:23 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10949"; a="1113691808" X-IronPort-AV: E=Sophos;i="6.04,185,1695711600"; d="scan'208";a="1113691808" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga005.fm.intel.com with ESMTP; 10 Jan 2024 20:10:21 -0800 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com, joao.m.martins@oracle.com, xin.zeng@intel.com, yan.y.zhao@intel.com, j.granados@samsung.com, binbin.wu@linux.intel.com Subject: [PATCH v11 1/8] iommu: Add cache_invalidate_user op Date: Wed, 10 Jan 2024 20:10:08 -0800 Message-Id: <20240111041015.47920-2-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240111041015.47920-1-yi.l.liu@intel.com> References: <20240111041015.47920-1-yi.l.liu@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787765736291431058 X-GMAIL-MSGID: 1787765736291431058 From: Lu Baolu The updates of the PTEs in the nested page table will be propagated to the hardware caches. Add a new domain op cache_invalidate_user 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. Then, pass in invalidation requests in form of a user data array containing a number of invalidation data entries. Reviewed-by: Kevin Tian Signed-off-by: Lu Baolu Signed-off-by: Nicolin Chen Signed-off-by: Yi Liu --- include/linux/iommu.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 6291aa7b079b..93c0d12dd047 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -284,6 +284,23 @@ struct iommu_user_data { size_t len; }; +/** + * struct iommu_user_data_array - iommu driver specific user space data array + * @type: The data type of all the entries in the user buffer array + * @uptr: Pointer to the user buffer array + * @entry_len: The fixed-width length of an entry in the array, in bytes + * @entry_num: The number of total entries in the array + * + * The user buffer includes an array of requests with format defined in + * include/uapi/linux/iommufd.h + */ +struct iommu_user_data_array { + unsigned int type; + void __user *uptr; + size_t entry_len; + u32 entry_num; +}; + /** * __iommu_copy_struct_from_user - Copy iommu driver specific user space data * @dst_data: Pointer to an iommu driver specific user data that is defined in @@ -440,6 +457,13 @@ 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 cache for user space IO page table. + * The @domain must be IOMMU_DOMAIN_NESTED. The @array + * passes in the cache invalidation requests, in form + * of a driver data structure. The driver must update + * array->entry_num to report the number of handled + * invalidation requests. The driver data structure + * must be defined 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 @@ -465,6 +489,8 @@ 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, + struct iommu_user_data_array *array); phys_addr_t (*iova_to_phys)(struct iommu_domain *domain, dma_addr_t iova); From patchwork Thu Jan 11 04:10:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 187158 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2411:b0:101:2151:f287 with SMTP id m17csp1219183dyi; Wed, 10 Jan 2024 20:11:54 -0800 (PST) X-Google-Smtp-Source: AGHT+IF+5yqhEKT3sbx8yiFE79QgU0HZRy77nps77w0aMSd2LRu8tCztKYWz9/HAtUdwTA3IzARS X-Received: by 2002:a05:6830:2b13:b0:6dc:2e77:42c8 with SMTP id l19-20020a0568302b1300b006dc2e7742c8mr848120otv.29.1704946313924; Wed, 10 Jan 2024 20:11:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704946313; cv=none; d=google.com; s=arc-20160816; b=k4+B7+Y6OuFbO1JQEYYnmoD7gAc8A5klhDNhUxKWaSRyoJHAnOHNYYBjgPDQcciF/K eVs9+WI75nGdZsY8khIPLuu/XN5j8gTzZEo1jIlHUzD9gQKbg3AHfV+3a0RGGxFZnD54 bPZ95qerUNcaMH7nI2aMc+f1qnp8hFPXp6JEU+9ZSj4+tnLxZeS21OlnXNqdY/w+evN2 Zo/twLugTsW6H1Ee/pq9oJqa4k0BF9DTYW1ofYnJCLxUMtxYCaq18LE6/DItRL6BHe+N TYTTl24RAXnYtK3TafKkUrF2z/PmAc7Uu3rzl2PyJRPHfDk6dnqyB7M2aCNs0eidI4IK /TZQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=wAwlr7sQ/I48JWhbjP2tWzBDi/kfFU0Ofrr6FCUbrp4=; fh=aa7ajXSXkJe0gRz4CDJDDWs+jFd1z8IECXwIf+VZeM8=; b=PBJlXWBvzHHtQtRSxNVBwm6VDdQ8EIa5B46nnBYsuDPqzlTZsOunp7S1EswzF1xyTj PdJAc8+ZdugUPvhYX6/NRa1ookgGqRz4Dys/PDFWPXr9wdxq809ZTE2Tw9wRYiFwXN08 jm76oPyDzZ4RWp4WFh07lQkyj/mLykrSOYtQIphuG0yoepDcv81qq04qOkOkfy1rNRih zH+8eJH2hGSjdWvKTGg9U7PXRZz1G/KJ0GH7BUlBJjR6TGNnPymDEz9QAJZSosIDoVo3 Ode0KaxMEZB5i0yAYdM2+ibAfKmUGcQHobKJGViWWYCIk2YHIqyZm3VXG6NK8TFJg6vX afWw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="fLVYx2H/"; spf=pass (google.com: domain of linux-kernel+bounces-23050-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-23050-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id l69-20020a638848000000b005cdff5fb163si238058pgd.333.2024.01.10.20.11.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 20:11:53 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-23050-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="fLVYx2H/"; spf=pass (google.com: domain of linux-kernel+bounces-23050-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-23050-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id DF108B263AE for ; Thu, 11 Jan 2024 04:11:45 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C23D5CA6F; Thu, 11 Jan 2024 04:10:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="fLVYx2H/" Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.20]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 967A0110F; Thu, 11 Jan 2024 04:10:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1704946225; x=1736482225; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=cNouUsps1r+dmbqyfgc6rMPqSKe8Zay6ScVLkagmhB4=; b=fLVYx2H/WOIfvLZcMfKwg7K9Og0BaYzJrNY4fh6itdGtvT+CDa4G0TUG B/r+z1TdUP9wATvuFDfomPU7Wt8w80cQSNfbd3jFY4KOZ2rcA0W7MJiML QxirbBkb1flVsjlA1WTeuy3XPHpCKcBnpcJIoZCcCMwVVLcfhaZWkYcwL NNa1hXswR+qg6bdocg9xIylQxK4c7Ig/+DU9KRio7bDikr9/TTUz9NL5z K+ifq4k8RL2oetwzjUAW8emi0hE8kKoOf0K2LETWiq5/sYjtCxYMVwhsI rYiIy68P3rNMTDle/mPLWlP8iYYGQOqS7UVfORgaQe4cKVYE6KWjp5k/b w==; X-IronPort-AV: E=McAfee;i="6600,9927,10949"; a="389167627" X-IronPort-AV: E=Sophos;i="6.04,185,1695711600"; d="scan'208";a="389167627" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jan 2024 20:10:24 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10949"; a="1113691828" X-IronPort-AV: E=Sophos;i="6.04,185,1695711600"; d="scan'208";a="1113691828" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga005.fm.intel.com with ESMTP; 10 Jan 2024 20:10:23 -0800 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com, joao.m.martins@oracle.com, xin.zeng@intel.com, yan.y.zhao@intel.com, j.granados@samsung.com, binbin.wu@linux.intel.com Subject: [PATCH v11 2/8] iommufd: Add IOMMU_HWPT_INVALIDATE Date: Wed, 10 Jan 2024 20:10:09 -0800 Message-Id: <20240111041015.47920-3-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240111041015.47920-1-yi.l.liu@intel.com> References: <20240111041015.47920-1-yi.l.liu@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787765785920081428 X-GMAIL-MSGID: 1787765785920081428 In nested translation, the stage-1 page table is user-managed but cached by the IOMMU hardware, so an update on present page table entries in the stage-1 page table should be followed with a cache invalidation. Add an IOMMU_HWPT_INVALIDATE ioctl to support such a cache invalidation. It takes hwpt_id to specify the iommu_domain, and a multi-entry array to support multiple invalidation data in one ioctl. enum iommu_hwpt_invalidate_data_type is defined to tag the data type of the entries in the multi-entry array. Reviewed-by: Kevin Tian Co-developed-by: Nicolin Chen Signed-off-by: Nicolin Chen Signed-off-by: Yi Liu --- drivers/iommu/iommufd/hw_pagetable.c | 41 +++++++++++++++++++++++ drivers/iommu/iommufd/iommufd_private.h | 10 ++++++ drivers/iommu/iommufd/main.c | 3 ++ include/uapi/linux/iommufd.h | 43 +++++++++++++++++++++++++ 4 files changed, 97 insertions(+) diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index cbb5df0a6c32..4e8711f19f72 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -371,3 +371,44 @@ int iommufd_hwpt_get_dirty_bitmap(struct iommufd_ucmd *ucmd) iommufd_put_object(ucmd->ictx, &hwpt_paging->common.obj); return rc; } + +int iommufd_hwpt_invalidate(struct iommufd_ucmd *ucmd) +{ + struct iommu_hwpt_invalidate *cmd = ucmd->cmd; + struct iommu_user_data_array data_array = { + .type = cmd->data_type, + .uptr = u64_to_user_ptr(cmd->data_uptr), + .entry_len = cmd->entry_len, + .entry_num = cmd->entry_num, + }; + struct iommufd_hw_pagetable *hwpt; + u32 done_num = 0; + int rc; + + if (cmd->__reserved) { + rc = -EOPNOTSUPP; + goto out; + } + + if (cmd->entry_num && (!cmd->data_uptr || !cmd->entry_len)) { + rc = -EINVAL; + goto out; + } + + hwpt = iommufd_get_hwpt_nested(ucmd, cmd->hwpt_id); + if (IS_ERR(hwpt)) { + rc = PTR_ERR(hwpt); + goto out; + } + + rc = hwpt->domain->ops->cache_invalidate_user(hwpt->domain, + &data_array); + done_num = data_array.entry_num; + + iommufd_put_object(ucmd->ictx, &hwpt->obj); +out: + cmd->entry_num = done_num; + if (iommufd_ucmd_respond(ucmd, sizeof(*cmd))) + return -EFAULT; + return rc; +} diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index abae041e256f..991f864d1f9b 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -328,6 +328,15 @@ iommufd_get_hwpt_paging(struct iommufd_ucmd *ucmd, u32 id) IOMMUFD_OBJ_HWPT_PAGING), struct iommufd_hwpt_paging, common.obj); } + +static inline struct iommufd_hw_pagetable * +iommufd_get_hwpt_nested(struct iommufd_ucmd *ucmd, u32 id) +{ + return container_of(iommufd_get_object(ucmd->ictx, id, + IOMMUFD_OBJ_HWPT_NESTED), + struct iommufd_hw_pagetable, obj); +} + int iommufd_hwpt_set_dirty_tracking(struct iommufd_ucmd *ucmd); int iommufd_hwpt_get_dirty_bitmap(struct iommufd_ucmd *ucmd); @@ -345,6 +354,7 @@ void iommufd_hwpt_paging_abort(struct iommufd_object *obj); void iommufd_hwpt_nested_destroy(struct iommufd_object *obj); void iommufd_hwpt_nested_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) diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index c9091e46d208..39b32932c61e 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -322,6 +322,7 @@ union ucmd_buffer { struct iommu_hw_info info; struct iommu_hwpt_alloc hwpt; struct iommu_hwpt_get_dirty_bitmap get_dirty_bitmap; + struct iommu_hwpt_invalidate cache; struct iommu_hwpt_set_dirty_tracking set_dirty_tracking; struct iommu_ioas_alloc alloc; struct iommu_ioas_allow_iovas allow_iovas; @@ -360,6 +361,8 @@ static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = { __reserved), IOCTL_OP(IOMMU_HWPT_GET_DIRTY_BITMAP, iommufd_hwpt_get_dirty_bitmap, struct iommu_hwpt_get_dirty_bitmap, data), + IOCTL_OP(IOMMU_HWPT_INVALIDATE, iommufd_hwpt_invalidate, + struct iommu_hwpt_invalidate, __reserved), IOCTL_OP(IOMMU_HWPT_SET_DIRTY_TRACKING, iommufd_hwpt_set_dirty_tracking, struct iommu_hwpt_set_dirty_tracking, __reserved), IOCTL_OP(IOMMU_IOAS_ALLOC, iommufd_ioas_alloc_ioctl, diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index 0b2bc6252e2c..824560c50ec6 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -49,6 +49,7 @@ enum { IOMMUFD_CMD_GET_HW_INFO, IOMMUFD_CMD_HWPT_SET_DIRTY_TRACKING, IOMMUFD_CMD_HWPT_GET_DIRTY_BITMAP, + IOMMUFD_CMD_HWPT_INVALIDATE, }; /** @@ -613,4 +614,46 @@ struct iommu_hwpt_get_dirty_bitmap { #define IOMMU_HWPT_GET_DIRTY_BITMAP _IO(IOMMUFD_TYPE, \ IOMMUFD_CMD_HWPT_GET_DIRTY_BITMAP) +/** + * enum iommu_hwpt_invalidate_data_type - IOMMU HWPT Cache Invalidation + * Data Type + * @IOMMU_HWPT_INVALIDATE_DATA_VTD_S1: Invalidation data for VTD_S1 + */ +enum iommu_hwpt_invalidate_data_type { + IOMMU_HWPT_INVALIDATE_DATA_VTD_S1, +}; + +/** + * struct iommu_hwpt_invalidate - ioctl(IOMMU_HWPT_INVALIDATE) + * @size: sizeof(struct iommu_hwpt_invalidate) + * @hwpt_id: ID of a nested HWPT for cache invalidation + * @data_uptr: User pointer to an array of driver-specific cache invalidation + * data. + * @data_type: One of enum iommu_hwpt_invalidate_data_type, defining the data + * type of all the entries in the invalidation request array. It + * should be a type supported by the hwpt pointed by @hwpt_id. + * @entry_len: Length (in bytes) of a request entry in the request array + * @entry_num: Input the number of cache invalidation requests in the array. + * Output the number of requests successfully handled by kernel. + * @__reserved: Must be 0. + * + * Invalidate the iommu cache for user-managed page table. Modifications on a + * user-managed page table should be followed by this operation to sync cache. + * Each ioctl can support one or more cache invalidation requests in the array + * that has a total size of @entry_len * @entry_num. + * + * An empty invalidation request array by setting @entry_num==0 is allowed, and + * @entry_len and @data_uptr would be ignored in this case. This can be used to + * check if the given @data_type is supported or not by kernel. + */ +struct iommu_hwpt_invalidate { + __u32 size; + __u32 hwpt_id; + __aligned_u64 data_uptr; + __u32 data_type; + __u32 entry_len; + __u32 entry_num; + __u32 __reserved; +}; +#define IOMMU_HWPT_INVALIDATE _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HWPT_INVALIDATE) #endif From patchwork Thu Jan 11 04:10:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 187159 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2411:b0:101:2151:f287 with SMTP id m17csp1219208dyi; Wed, 10 Jan 2024 20:11:59 -0800 (PST) X-Google-Smtp-Source: AGHT+IGIPCZ2XiUyNIgOj+kJpTGE8YFdHp2QugW6ZOHOWhZyWv/0XpdEheFfiDIvpujxcWII+8dZ X-Received: by 2002:a17:90a:c918:b0:28d:c51d:37b with SMTP id v24-20020a17090ac91800b0028dc51d037bmr432558pjt.64.1704946319004; Wed, 10 Jan 2024 20:11:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704946318; cv=none; d=google.com; s=arc-20160816; b=PAId24SPmjZQk9LlG8VOXaa9JERFTDyBJqaFqw/gp6KbuHzA+rbgpAg+b5mw8E/DVH cc+8AfgTFRtQAktvRzUiUyOLHQkgvPz9CXS60PpOmBszXG0BsQuNQpLmEobf8tAIGxFE 5WDNi4udvXW7Zv2DE5EY+R2LTiItzDuIsEHw3i2f38r7b/WKqWY9dXAoNTfeG+kdreJf qmplUzo8TGnOT2GW1TOjnnmjeIklpQUna2wdwGeDBrIQwP91eVM8W+tq2kEaA/vHe3Uc bcqv9KCj8v/tHLpf8V8FFdZXvSzNQH7LeXLDlSP9KAb0sM/qjtijhbI86Mh13FEF3H+K lI1Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=Jwuyq/0XKxP8UkepkTVktSPi1mZsQkVEMKyHrWehr3Q=; fh=aa7ajXSXkJe0gRz4CDJDDWs+jFd1z8IECXwIf+VZeM8=; b=q/luJc7lmJBt6tkuNCLWUZXxL5JpI1owgsMOZE77uHHffXHJupyMmGhgaFHT5FVaxZ d2EaZQnJ9NVzMyzxWEM03oIlAdGKyVsaSAbRD/izAZPQLPzzfaQjLIe0a2+zm6y/jnGj 3yKgcE/Y0Eh6ebnVs3EVV1P/hTYhEdvIHo/QdADTH7B8tceSM0hi9SREiO3cSwWiWtio SDnJKh6RcGNXoydXAMF47ltNokLHtoIlFAKy66h73oIZX4a6vkLzmtcd+Z7obJSrIFj8 /YhSMyBo3sMex5/kjAwkYnsMBprm8LD1vLSQ++7Xyumnt0fi/QlsFx67mpFdtywjOnYx e1LQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=QQyJihhQ; spf=pass (google.com: domain of linux-kernel+bounces-23051-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-23051-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id e88-20020a17090a6fe100b0028bc7d0a605si2847308pjk.131.2024.01.10.20.11.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 20:11:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-23051-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=QQyJihhQ; spf=pass (google.com: domain of linux-kernel+bounces-23051-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-23051-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id C1D5B288705 for ; Thu, 11 Jan 2024 04:11:58 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AC9F6D2FD; Thu, 11 Jan 2024 04:10:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="QQyJihhQ" Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.20]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6DFFF468E; Thu, 11 Jan 2024 04:10:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1704946227; x=1736482227; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=7jTK0zsrD6iUZ/k8/QLoj03D3txfzWkMPindf/3ehAE=; b=QQyJihhQerTcRzYUQGugYghfNodVvJVSWskfzT3ErNIkReR8g2cM8E8Z aHEmeLVitaBcaLR/oxoDDzDTbm3yffnKT5qaEd+0S4Sdq3zDzbAqc3MBg Oivujk9DsccwkkCoNlfTmrV2UToHvDST90m8NMOnU2WPy5eEyq/ut1vGG iF9CI46SDddeUOeCqCcA62s0d/vz2s305VgaEyqfs8Os92btLpq+LmGJh it3sO3KbfEecEAQAk2ujzsVoj9u701cnLMFMKAAQok86BQAXOSyKC9y3d KRMPYoE0eg3XS7ic7r/BTciiV7ITVM6KJt8+DIYIbJ8Jxqjo1G32iz9uj w==; X-IronPort-AV: E=McAfee;i="6600,9927,10949"; a="389167639" X-IronPort-AV: E=Sophos;i="6.04,185,1695711600"; d="scan'208";a="389167639" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jan 2024 20:10:26 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10949"; a="1113691842" X-IronPort-AV: E=Sophos;i="6.04,185,1695711600"; d="scan'208";a="1113691842" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga005.fm.intel.com with ESMTP; 10 Jan 2024 20:10:25 -0800 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com, joao.m.martins@oracle.com, xin.zeng@intel.com, yan.y.zhao@intel.com, j.granados@samsung.com, binbin.wu@linux.intel.com Subject: [PATCH v11 3/8] iommu: Add iommu_copy_struct_from_user_array helper Date: Wed, 10 Jan 2024 20:10:10 -0800 Message-Id: <20240111041015.47920-4-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240111041015.47920-1-yi.l.liu@intel.com> References: <20240111041015.47920-1-yi.l.liu@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787765791675371276 X-GMAIL-MSGID: 1787765791675371276 From: Nicolin Chen Wrap up the data pointer/num sanity and __iommu_copy_struct_from_user call for iommu drivers to copy driver specific data at a specific location in the struct iommu_user_data_array. And expect it to be used in cache_invalidate_user ops for example. Reviewed-by: Kevin Tian Signed-off-by: Nicolin Chen Co-developed-by: Yi Liu Signed-off-by: Yi Liu --- include/linux/iommu.h | 53 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 93c0d12dd047..580ba74a3d5d 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -341,6 +341,59 @@ static inline int __iommu_copy_struct_from_user( sizeof(*kdst), \ offsetofend(typeof(*kdst), min_last)) +/** + * __iommu_copy_struct_from_user_array - Copy iommu driver specific user space + * data from an iommu_user_data_array + * @dst_data: Pointer to an iommu driver specific user data that is defined in + * include/uapi/linux/iommufd.h + * @src_array: Pointer to a struct iommu_user_data_array for a user space array + * @data_type: The data type of the @dst_data. Must match with @src_array.type + * @index: Index to the location in the array to copy user data from + * @data_len: Length of current user data structure, i.e. sizeof(struct _dst) + * @min_len: Initial length of user data structure for backward compatibility. + * This should be offsetofend using the last member in the user data + * struct that was initially added to include/uapi/linux/iommufd.h + */ +static inline int +__iommu_copy_struct_from_user_array(void *dst_data, + const struct iommu_user_data_array *src_array, + unsigned int data_type, unsigned int index, + size_t data_len, size_t min_len) +{ + struct iommu_user_data src_data; + + if (WARN_ON(!src_array || index >= src_array->entry_num)) + return -EINVAL; + if (!src_array->entry_num) + return -EINVAL; + src_data.uptr = src_array->uptr + src_array->entry_len * index; + src_data.len = src_array->entry_len; + src_data.type = src_array->type; + + return __iommu_copy_struct_from_user(dst_data, &src_data, data_type, + data_len, min_len); +} + +/** + * iommu_copy_struct_from_user_array - Copy iommu driver specific user space + * data from an iommu_user_data_array + * @kdst: Pointer to an iommu driver specific user data that is defined in + * include/uapi/linux/iommufd.h + * @user_array: Pointer to a struct iommu_user_data_array for a user space + * array + * @data_type: The data type of the @kdst. Must match with @user_array->type + * @index: Index to the location in the array to copy user data from + * @min_last: The last member of the data structure @kdst points in the + * initial version. + * Return 0 for success, otherwise -error. + */ +#define iommu_copy_struct_from_user_array(kdst, user_array, data_type, \ + index, min_last) \ + __iommu_copy_struct_from_user_array(kdst, user_array, data_type, \ + index, sizeof(*kdst), \ + offsetofend(typeof(*kdst), \ + min_last)) + /** * struct iommu_ops - iommu ops and capabilities * @capable: check capability From patchwork Thu Jan 11 04:10:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 187160 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2411:b0:101:2151:f287 with SMTP id m17csp1219258dyi; Wed, 10 Jan 2024 20:12:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IF7QiNH+icrtK3OhLkHmfMRu8L78GaZFBwQ94cxkxO+pPGG2hGLRPkYSB5keyVVigUEq7lj X-Received: by 2002:a05:6358:7e42:b0:175:76bd:2c36 with SMTP id p2-20020a0563587e4200b0017576bd2c36mr754915rwm.37.1704946326790; Wed, 10 Jan 2024 20:12:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704946326; cv=none; d=google.com; s=arc-20160816; b=B7PxtsV4B5GZwKn9rBfaFm3vyZLzDwu4bAW0HjSkyUnNtDtd/uIZVg1cJ7Ft3opItg GcFL9WX8XsJWCJHRWaLCUU2b3C9x0qEosQSGxitw4YeYlOH94s7sf32N6Zx3sus1x6Fr 2fiRl0NVU+kMStgi0p74X+JEwh8LmqyS/lQOsCKCBJaq5ThNf/m1NOD+exdkdr74u8bV BtZkV4hhA2f9LTDDeou9ANO4lchMnusiy3/Ge5SIfnj/9OovciwCjrl4w6lgTjByOot+ nr5A++607eRrM4YqjpmkpmWPoTihFgOUUrTvnPoZdT4ne4H3L6k/LdpednpAfrgyT7XM 0Tmg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=JFzCBEpQD8yDOKlEdyHl4z5fKVBln57rI3WX0tRK/3w=; fh=aa7ajXSXkJe0gRz4CDJDDWs+jFd1z8IECXwIf+VZeM8=; b=yG8NHY9JdqyKU4+YoDhP10NJ0s/mge05KLgKpkyOX4n5CQhgBcSYduSzhe33SuLExy MqB3czjnS+B/BXPgy8P8+OcWSmRLv1yX27seZXee8P+1Y3rOLnTZSGKMaPc7sSZNLlnD c+vaFC8ocCO2ksT1lYoDh+V6J9J/0xAvySn9yEREf6aydyM0ZNzoQcPOypVbZzBJBlQg uFhJqErGVjKIoOc1MzKcKL4fXCjPlPfkkS9THqZjosGtUWo80gAKxbtXHiCYem+f3MUY gwxwjUe5EG3xxN4wzA9ZGc1mlVAESIiXBKJLB+ZoQEg4nHzQu3FtTioLeGrbBxdWnQu3 nxvw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=e5YZxkBb; spf=pass (google.com: domain of linux-kernel+bounces-23052-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-23052-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id h13-20020a17090aa88d00b0028dbd5b9f76si263038pjq.116.2024.01.10.20.12.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 20:12:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-23052-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=e5YZxkBb; spf=pass (google.com: domain of linux-kernel+bounces-23052-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-23052-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 7294428637A for ; Thu, 11 Jan 2024 04:12:06 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EEB1DDDA6; Thu, 11 Jan 2024 04:10:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="e5YZxkBb" Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.20]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AEF746139; Thu, 11 Jan 2024 04:10:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1704946228; x=1736482228; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=jR0MOQ0kqVSVT6BksVmkq7HuXTI6cH5F4jkXr2f/2Kg=; b=e5YZxkBbbPGlNJfrm3fZHvnI2HlnrNbr5pf8sGd/uxjsQsPxPLr7SePe tlXQbQNr+qkSe/8l59BYOJnMRCEu9Coo2a2L1I4pKVURoPfMVN9iOuSX+ p9ESdCUtAiLntpaRials0aJ4q+1UrSMbgAu7Y9fkaX1RxKOL45yJ290CH 7b2v49d44vg8w3QvHvvndbUYuN+sIM2GfGRUJZbWwEoKEncYjhioCwl7Z FyIu2yuqq2wYRYLiWt9GQJmGnYW8fvfI4s4mQYTxPS+zQkTLKgAIK66Lx VpzEctFUGNVzrG/8CIsgnC7Igl2Sb+h39raAOLMDHRHhkKEe4YXGJ9HXw A==; X-IronPort-AV: E=McAfee;i="6600,9927,10949"; a="389167653" X-IronPort-AV: E=Sophos;i="6.04,185,1695711600"; d="scan'208";a="389167653" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jan 2024 20:10:28 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10949"; a="1113691862" X-IronPort-AV: E=Sophos;i="6.04,185,1695711600"; d="scan'208";a="1113691862" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga005.fm.intel.com with ESMTP; 10 Jan 2024 20:10:26 -0800 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com, joao.m.martins@oracle.com, xin.zeng@intel.com, yan.y.zhao@intel.com, j.granados@samsung.com, binbin.wu@linux.intel.com Subject: [PATCH v11 4/8] iommufd/selftest: Add mock_domain_cache_invalidate_user support Date: Wed, 10 Jan 2024 20:10:11 -0800 Message-Id: <20240111041015.47920-5-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240111041015.47920-1-yi.l.liu@intel.com> References: <20240111041015.47920-1-yi.l.liu@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787765799665494485 X-GMAIL-MSGID: 1787765799665494485 From: Nicolin Chen Add mock_domain_cache_invalidate_user() data structure to support user space selftest program to cover user cache invalidation pathway. Reviewed-by: Kevin Tian Signed-off-by: Nicolin Chen Co-developed-by: Yi Liu Signed-off-by: Yi Liu --- drivers/iommu/iommufd/iommufd_test.h | 18 ++++++++++ drivers/iommu/iommufd/selftest.c | 50 ++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/drivers/iommu/iommufd/iommufd_test.h b/drivers/iommu/iommufd/iommufd_test.h index 7910fbe1962d..09dfc8aa65c4 100644 --- a/drivers/iommu/iommufd/iommufd_test.h +++ b/drivers/iommu/iommufd/iommufd_test.h @@ -148,4 +148,22 @@ struct iommu_hwpt_selftest { __u32 iotlb; }; +/* Should not be equal to any defined value in enum iommu_hwpt_invalidate_data_type */ +#define IOMMU_HWPT_INVALIDATE_DATA_SELFTEST 0xdeadbeef +#define IOMMU_HWPT_INVALIDATE_DATA_SELFTEST_INVALID 0xdadbeef + +/** + * struct iommu_hwpt_invalidate_selftest - Invalidation data for Mock driver + * (IOMMU_HWPT_INVALIDATE_DATA_SELFTEST) + * @flags: Invalidate flags + * @iotlb_id: Invalidate iotlb entry index + * + * If IOMMU_TEST_INVALIDATE_ALL is set in @flags, @iotlb_id will be ignored + */ +struct iommu_hwpt_invalidate_selftest { +#define IOMMU_TEST_INVALIDATE_FLAG_ALL (1 << 0) + __u32 flags; + __u32 iotlb_id; +}; + #endif diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c index 022ef8f55088..23879135d1c3 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -473,9 +473,59 @@ static void mock_domain_free_nested(struct iommu_domain *domain) kfree(mock_nested); } +static int +mock_domain_cache_invalidate_user(struct iommu_domain *domain, + struct iommu_user_data_array *array) +{ + struct mock_iommu_domain_nested *mock_nested = + container_of(domain, struct mock_iommu_domain_nested, domain); + struct iommu_hwpt_invalidate_selftest inv; + u32 processed = 0; + int i = 0, j; + int rc = 0; + + if (array->type != IOMMU_HWPT_INVALIDATE_DATA_SELFTEST) { + rc = -EINVAL; + goto out; + } + + for ( ; i < array->entry_num; i++) { + rc = iommu_copy_struct_from_user_array(&inv, array, + IOMMU_HWPT_INVALIDATE_DATA_SELFTEST, + i, iotlb_id); + if (rc) + break; + + if (inv.flags & ~IOMMU_TEST_INVALIDATE_FLAG_ALL) { + rc = -EOPNOTSUPP; + break; + } + + if (inv.iotlb_id > MOCK_NESTED_DOMAIN_IOTLB_ID_MAX) { + rc = -EINVAL; + break; + } + + if (inv.flags & IOMMU_TEST_INVALIDATE_FLAG_ALL) { + /* Invalidate all mock iotlb entries and ignore iotlb_id */ + for (j = 0; j < MOCK_NESTED_DOMAIN_IOTLB_NUM; j++) + mock_nested->iotlb[j] = 0; + } else { + mock_nested->iotlb[inv.iotlb_id] = 0; + } + + processed++; + } + +out: + array->entry_num = processed; + return rc; +} + static struct iommu_domain_ops domain_nested_ops = { .free = mock_domain_free_nested, .attach_dev = mock_domain_nop_attach, + .cache_invalidate_user = mock_domain_cache_invalidate_user, }; static inline struct iommufd_hw_pagetable * From patchwork Thu Jan 11 04:10:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 187161 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2411:b0:101:2151:f287 with SMTP id m17csp1219488dyi; Wed, 10 Jan 2024 20:12:52 -0800 (PST) X-Google-Smtp-Source: AGHT+IGxEdxvyw41KhS/ShV7I6YJCF3WlPNJ8LFrphPJa+G+PoDUxA3BGap7imFgxqXbm1xT0B5G X-Received: by 2002:ac8:5fd4:0:b0:429:9fd7:5747 with SMTP id k20-20020ac85fd4000000b004299fd75747mr55048qta.96.1704946371858; Wed, 10 Jan 2024 20:12:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704946371; cv=none; d=google.com; s=arc-20160816; b=AfkQXlCTfT+0ip8fBIwgyPlJg7SxkA+viIFdapvPXWiRHXkfTHBs4CH5ScRbM248HZ DXYusUWHOCZtuO7pWMcg8PXTs0sg4aLNhnfR1O6HELPMfnszRySprxXvveG9oK79JE5t Ygj1NaDyRUTU1D3SNT+8LsFiazud6oMpjskFvjcLt288LzE/AbkoX61e5AG/Qyzx6sn2 y/oi4Y8Zax1FBvTfSVzec0RVMk8pwEuqIr39j2Dq7g4rz7TPjzwqqutk6+JizgRP0Un7 VK41emBacdYPEoMnz5IWfPDXGVykmVVFiNMXwdC7nFJ2ZQnFPcFJX9MTNZo9pmQglynJ oSsg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=pM9FQmX0OYWJbqICU/d3SLBfjzu/89a+zqvEPNcaJXE=; fh=aa7ajXSXkJe0gRz4CDJDDWs+jFd1z8IECXwIf+VZeM8=; b=xMbK6FHCa8/kHyWl5OFvFRTrOAwdoR/P4/snAmKCir54bgStAbp5vRoMh19h+g/OWQ XmmmZinWl0C/oy6nzcL7eG+Kj0OtBlhumL60VLpPocx1B8duEiAUl4DBsFbv+DAw/buJ P7XDTI2szbLlfwq3+84ctJB0nmnYxfGYpCK+GHbVNEESjB2zhhMaiKJCGl/vmLMKhkMd 6Cv39paXWm9eQyLjsNOh1Ls0fERveKxLvSIGKJCxZ3nDvB+ep2/oYWqZk5XS1F0hkQ8c nuGqCVbnapfEzzlDIZuSSStlD8MGC1AjtdTVFwOOYhF06zVdqyR2vcPJtwJIRCDZDj8N tpeA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=hitKMs4h; spf=pass (google.com: domain of linux-kernel+bounces-23053-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-23053-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id f1-20020ac87f01000000b00423dccd9929si229006qtk.495.2024.01.10.20.12.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 20:12:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-23053-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=hitKMs4h; spf=pass (google.com: domain of linux-kernel+bounces-23053-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-23053-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 970EF1C2308F for ; Thu, 11 Jan 2024 04:12:51 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 21F44F9C4; Thu, 11 Jan 2024 04:10:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="hitKMs4h" Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.20]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 643E7D292; Thu, 11 Jan 2024 04:10:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1704946231; x=1736482231; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=olFc0AFQKAgKNB/yE+Agwt8M3F5B0HjtcudkOSJuixk=; b=hitKMs4hRJs9hUlm/RMoJAf9f4zN07RnlGxXPzB1YYIv6M4BSZAictD4 vyT5fT6WXEDgprHotrGqM2PJRnVNIyBtGG2bnk0fER5UaMdIAwlYGjQgL kp0mQ6GtO+Pdna2feZQRPWuqULEAIXKcbpkIqif0kRFU6KU5fW/CGcYMA C9f/i308b/2uG7eUjWVpomDtWad6DfgNkIwxzSTpcnq9kW2q3SS6WvAYw P1ydLyBZfnlqwCU+X5zVmJCOdArDhj7urQ88h8whchlBAMa4LAVCQEa7M eCshwwsdk+lgtI3s7m8akY61zd3F+zPlOaxMl9QVsrioB6ls9PbOtGHA1 A==; X-IronPort-AV: E=McAfee;i="6600,9927,10949"; a="389167666" X-IronPort-AV: E=Sophos;i="6.04,185,1695711600"; d="scan'208";a="389167666" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jan 2024 20:10:29 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10949"; a="1113691876" X-IronPort-AV: E=Sophos;i="6.04,185,1695711600"; d="scan'208";a="1113691876" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga005.fm.intel.com with ESMTP; 10 Jan 2024 20:10:28 -0800 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com, joao.m.martins@oracle.com, xin.zeng@intel.com, yan.y.zhao@intel.com, j.granados@samsung.com, binbin.wu@linux.intel.com Subject: [PATCH v11 5/8] iommufd/selftest: Add IOMMU_TEST_OP_MD_CHECK_IOTLB test op Date: Wed, 10 Jan 2024 20:10:12 -0800 Message-Id: <20240111041015.47920-6-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240111041015.47920-1-yi.l.liu@intel.com> References: <20240111041015.47920-1-yi.l.liu@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787765846725646937 X-GMAIL-MSGID: 1787765846725646937 From: Nicolin Chen Allow to test whether IOTLB has been invalidated or not. Reviewed-by: Kevin Tian Signed-off-by: Nicolin Chen Signed-off-by: Yi Liu --- drivers/iommu/iommufd/iommufd_test.h | 5 ++++ drivers/iommu/iommufd/selftest.c | 26 +++++++++++++++++++ tools/testing/selftests/iommu/iommufd.c | 4 +++ tools/testing/selftests/iommu/iommufd_utils.h | 24 +++++++++++++++++ 4 files changed, 59 insertions(+) diff --git a/drivers/iommu/iommufd/iommufd_test.h b/drivers/iommu/iommufd/iommufd_test.h index 09dfc8aa65c4..482d4059f5db 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_MOCK_DOMAIN_FLAGS, IOMMU_TEST_OP_DIRTY, + IOMMU_TEST_OP_MD_CHECK_IOTLB, }; enum { @@ -121,6 +122,10 @@ struct iommu_test_cmd { __aligned_u64 uptr; __aligned_u64 out_nr_dirty; } dirty; + struct { + __u32 id; + __u32 iotlb; + } check_iotlb; }; __u32 last; }; diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c index 23879135d1c3..db648c81507f 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -843,6 +843,28 @@ 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, unsigned int iotlb_id, + u32 iotlb) +{ + struct mock_iommu_domain_nested *mock_nested; + struct iommufd_hw_pagetable *hwpt; + int rc = 0; + + hwpt = get_md_pagetable_nested(ucmd, mockpt_id, &mock_nested); + if (IS_ERR(hwpt)) + return PTR_ERR(hwpt); + + mock_nested = container_of(hwpt->domain, + struct mock_iommu_domain_nested, domain); + + if (iotlb_id > MOCK_NESTED_DOMAIN_IOTLB_ID_MAX || + mock_nested->iotlb[iotlb_id] != iotlb) + rc = -EINVAL; + iommufd_put_object(ucmd->ictx, &hwpt->obj); + return rc; +} + struct selftest_access { struct iommufd_access *access; struct file *file; @@ -1324,6 +1346,10 @@ 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.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 6ed328c863c4..c8763b880a16 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -330,6 +330,10 @@ TEST_F(iommufd_ioas, alloc_hwpt_nested) &nested_hwpt_id[1], IOMMU_HWPT_DATA_SELFTEST, &data, sizeof(data)); + test_cmd_hwpt_check_iotlb_all(nested_hwpt_id[0], + IOMMU_TEST_IOTLB_DEFAULT); + test_cmd_hwpt_check_iotlb_all(nested_hwpt_id[1], + IOMMU_TEST_IOTLB_DEFAULT); /* Negative test: a nested hwpt on top of a nested hwpt */ test_err_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 ad9202335656..fe0a0f566b67 100644 --- a/tools/testing/selftests/iommu/iommufd_utils.h +++ b/tools/testing/selftests/iommu/iommufd_utils.h @@ -195,6 +195,30 @@ static int _test_cmd_hwpt_alloc(int fd, __u32 device_id, __u32 pt_id, _test_cmd_hwpt_alloc(self->fd, device_id, pt_id, flags, \ hwpt_id, data_type, data, data_len)) +#define test_cmd_hwpt_check_iotlb(hwpt_id, iotlb_id, expected) \ + ({ \ + struct iommu_test_cmd test_cmd = { \ + .size = sizeof(test_cmd), \ + .op = IOMMU_TEST_OP_MD_CHECK_IOTLB, \ + .id = hwpt_id, \ + .check_iotlb = { \ + .id = iotlb_id, \ + .iotlb = expected, \ + }, \ + }; \ + ASSERT_EQ(0, \ + ioctl(self->fd, \ + _IOMMU_TEST_CMD(IOMMU_TEST_OP_MD_CHECK_IOTLB), \ + &test_cmd)); \ + }) + +#define test_cmd_hwpt_check_iotlb_all(hwpt_id, expected) \ + ({ \ + int i; \ + for (i = 0; i < MOCK_NESTED_DOMAIN_IOTLB_NUM; i++) \ + test_cmd_hwpt_check_iotlb(hwpt_id, i, expected); \ + }) + static int _test_cmd_access_replace_ioas(int fd, __u32 access_id, unsigned int ioas_id) { From patchwork Thu Jan 11 04:10:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 187164 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2411:b0:101:2151:f287 with SMTP id m17csp1221907dyi; Wed, 10 Jan 2024 20:20:57 -0800 (PST) X-Google-Smtp-Source: AGHT+IEEhOA65RNBxLJjz37NHLSFF5VwdbMNveBJTVz/qhdZQeOjIyYorDuA1COCCgPmlOFCp8hU X-Received: by 2002:a62:58c4:0:b0:6da:c8b7:d5ea with SMTP id m187-20020a6258c4000000b006dac8b7d5eamr616893pfb.14.1704946857702; Wed, 10 Jan 2024 20:20:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704946857; cv=none; d=google.com; s=arc-20160816; b=u5rvrwG7TbGOJhMf5ZHm4qo1derRy0pReAoIFN0ksN0rXSTKMNjJA8ajuanhO38UWB Yv91DZ9OY+XuxGCRfPTfnT3XrrMTTjzmUkwLuP8P8Gn1uH/cN7xDIu0HvzgYP5s3xPIP g4A/P+Z0rTLoaAJ3Xq2w9LA/B/rVZlIHafkJC2Xr5mofLO3KcXvkyEvk4cyKHmWLmOtX NLatyv9b9QZhUWlO2q4CjmXLi9buPUsP2FJ48r5udAy4ES0VGWgDyv5IYyhF1UYy8qL0 +I5an/1CheV9vgWWo6a0D6lRcvZ236rebBenrl4e7CYS+YWYGcTzm9eKTnUzn1qOr5Kt iHnQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=lUmdxEANs5zAJtbiksuJB9v59LuaMin5/Acg0ISP3+4=; fh=aa7ajXSXkJe0gRz4CDJDDWs+jFd1z8IECXwIf+VZeM8=; b=Az5gkAWVfyHN5phs1RXU6RGsodYN7eerTUwU7lD7/l07DmFAJ/JRwE4JHKpYMFMw9o CM5haDApHtF0j1QRNXgS0Sa2nvKUUXHpwNIIhcVB4y8QBiokVbZrKfv65//TZLLHSfaf wueFxM0FLNLdwpRQgoykQqQ0cyehAUJN4+TlRLOEPU+SljRPnidGMkeS1cv/xx9mdIc6 tKkhNGKPwt8w014qcDp0Xg7jrmQoOl14UEAlQAK9zSj+tIYqK9v576kRSHuK7iC4Z051 dvA33ZJAzCm45M1I/smxyGq5FGV4r3RSvN2n0AJITpW8vkp6h9su4Ij5CIGYZbAUWIp1 PJqQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=S7KRbgEJ; spf=pass (google.com: domain of linux-kernel+bounces-23055-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-23055-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id le1-20020a056a004fc100b006d9b8af64c8si239633pfb.70.2024.01.10.20.20.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 20:20:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-23055-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=S7KRbgEJ; spf=pass (google.com: domain of linux-kernel+bounces-23055-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-23055-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 2593928BB8F for ; Thu, 11 Jan 2024 04:13:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8D8B1FC18; Thu, 11 Jan 2024 04:10:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="S7KRbgEJ" Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.20]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2512C111D; Thu, 11 Jan 2024 04:10:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1704946233; x=1736482233; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=UVR8KhCZvbCUivxwHesEQRJAFLYF+tJ8B4S7sGVtRGU=; b=S7KRbgEJcmlG1GU3aoEy92DXA6T0YvWk1ovGkvVPzndikYTaN9R6/WiE +yZvNDlRSu9KYEGgVXUqbv/XP3wKISMjBdR9BLay1RYwGo7gFIodZNZQY XEpfuhfVjWWKoqMW0PesH/dDlbO/9cg95RlWcrlvL19Qgs5tN6hEH5VAp AIOjgjEw4kzGHaiWaHO6v1LUxysnMdRRT9K9ZBcewSgBonV76dbGmglFH gK6R0uQWDJsT09C8VsuQDCUUQpSkvOlUjS3oEnDjefmC4LgDLhLpz61Jc kcmCZPX0kCbXucUlgQobMSzfe+8V9AdVVsF7QnzLlt2brX+qju+B3Tdnp w==; X-IronPort-AV: E=McAfee;i="6600,9927,10949"; a="389167677" X-IronPort-AV: E=Sophos;i="6.04,185,1695711600"; d="scan'208";a="389167677" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jan 2024 20:10:31 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10949"; a="1113691889" X-IronPort-AV: E=Sophos;i="6.04,185,1695711600"; d="scan'208";a="1113691889" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga005.fm.intel.com with ESMTP; 10 Jan 2024 20:10:30 -0800 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com, joao.m.martins@oracle.com, xin.zeng@intel.com, yan.y.zhao@intel.com, j.granados@samsung.com, binbin.wu@linux.intel.com Subject: [PATCH v11 6/8] iommufd/selftest: Add coverage for IOMMU_HWPT_INVALIDATE ioctl Date: Wed, 10 Jan 2024 20:10:13 -0800 Message-Id: <20240111041015.47920-7-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240111041015.47920-1-yi.l.liu@intel.com> References: <20240111041015.47920-1-yi.l.liu@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787766356077546977 X-GMAIL-MSGID: 1787766356077546977 From: Nicolin Chen Add test cases for the IOMMU_HWPT_INVALIDATE ioctl and verify it by using the new IOMMU_TEST_OP_MD_CHECK_IOTLB. Reviewed-by: Kevin Tian Signed-off-by: Nicolin Chen Co-developed-by: Yi Liu Signed-off-by: Yi Liu --- tools/testing/selftests/iommu/iommufd.c | 148 ++++++++++++++++++ tools/testing/selftests/iommu/iommufd_utils.h | 33 ++++ 2 files changed, 181 insertions(+) diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c index c8763b880a16..1a881e7a21d1 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -116,6 +116,7 @@ TEST_F(iommufd, cmd_length) TEST_LENGTH(iommu_destroy, IOMMU_DESTROY, id); TEST_LENGTH(iommu_hw_info, IOMMU_GET_HW_INFO, __reserved); TEST_LENGTH(iommu_hwpt_alloc, IOMMU_HWPT_ALLOC, __reserved); + TEST_LENGTH(iommu_hwpt_invalidate, IOMMU_HWPT_INVALIDATE, __reserved); TEST_LENGTH(iommu_ioas_alloc, IOMMU_IOAS_ALLOC, out_ioas_id); TEST_LENGTH(iommu_ioas_iova_ranges, IOMMU_IOAS_IOVA_RANGES, out_iova_alignment); @@ -271,7 +272,9 @@ TEST_F(iommufd_ioas, alloc_hwpt_nested) struct iommu_hwpt_selftest data = { .iotlb = IOMMU_TEST_IOTLB_DEFAULT, }; + struct iommu_hwpt_invalidate_selftest inv_reqs[2] = {}; uint32_t nested_hwpt_id[2] = {}; + uint32_t num_inv; uint32_t parent_hwpt_id = 0; uint32_t parent_hwpt_id_not_work = 0; uint32_t test_hwpt_id = 0; @@ -344,6 +347,151 @@ TEST_F(iommufd_ioas, alloc_hwpt_nested) EXPECT_ERRNO(EBUSY, _test_ioctl_destroy(self->fd, parent_hwpt_id)); + /* hwpt_invalidate only supports a user-managed hwpt (nested) */ + num_inv = 1; + test_err_hwpt_invalidate(ENOENT, parent_hwpt_id, inv_reqs, + IOMMU_HWPT_INVALIDATE_DATA_SELFTEST, + sizeof(*inv_reqs), &num_inv); + assert(!num_inv); + + /* Check data_type by passing zero-length array */ + num_inv = 0; + test_cmd_hwpt_invalidate(nested_hwpt_id[0], inv_reqs, + IOMMU_HWPT_INVALIDATE_DATA_SELFTEST, + sizeof(*inv_reqs), &num_inv); + assert(!num_inv); + + /* Negative test: Invalid data_type */ + num_inv = 1; + test_err_hwpt_invalidate(EINVAL, nested_hwpt_id[0], inv_reqs, + IOMMU_HWPT_INVALIDATE_DATA_SELFTEST_INVALID, + sizeof(*inv_reqs), &num_inv); + assert(!num_inv); + + /* Negative test: structure size sanity */ + num_inv = 1; + test_err_hwpt_invalidate(EINVAL, nested_hwpt_id[0], inv_reqs, + IOMMU_HWPT_INVALIDATE_DATA_SELFTEST, + sizeof(*inv_reqs) + 1, &num_inv); + assert(!num_inv); + + num_inv = 1; + test_err_hwpt_invalidate(EINVAL, nested_hwpt_id[0], inv_reqs, + IOMMU_HWPT_INVALIDATE_DATA_SELFTEST, + 1, &num_inv); + assert(!num_inv); + + /* Negative test: invalid flag is passed */ + num_inv = 1; + inv_reqs[0].flags = 0xffffffff; + test_err_hwpt_invalidate(EOPNOTSUPP, nested_hwpt_id[0], inv_reqs, + IOMMU_HWPT_INVALIDATE_DATA_SELFTEST, + sizeof(*inv_reqs), &num_inv); + assert(!num_inv); + + /* Negative test: invalid data_uptr when array is not empty */ + num_inv = 1; + inv_reqs[0].flags = 0; + test_err_hwpt_invalidate(EINVAL, nested_hwpt_id[0], NULL, + IOMMU_HWPT_INVALIDATE_DATA_SELFTEST, + sizeof(*inv_reqs), &num_inv); + assert(!num_inv); + + /* Negative test: invalid entry_len when array is not empty */ + num_inv = 1; + inv_reqs[0].flags = 0; + test_err_hwpt_invalidate(EINVAL, nested_hwpt_id[0], inv_reqs, + IOMMU_HWPT_INVALIDATE_DATA_SELFTEST, + 0, &num_inv); + assert(!num_inv); + + /* Negative test: invalid iotlb_id */ + num_inv = 1; + inv_reqs[0].flags = 0; + inv_reqs[0].iotlb_id = MOCK_NESTED_DOMAIN_IOTLB_ID_MAX + 1; + test_err_hwpt_invalidate(EINVAL, nested_hwpt_id[0], inv_reqs, + IOMMU_HWPT_INVALIDATE_DATA_SELFTEST, + sizeof(*inv_reqs), &num_inv); + assert(!num_inv); + + /* + * Invalidate the 1st iotlb entry but fail the 2nd request + * due to invalid flags configuration in the 2nd request. + */ + num_inv = 2; + inv_reqs[0].flags = 0; + inv_reqs[0].iotlb_id = 0; + inv_reqs[1].flags = 0xffffffff; + inv_reqs[1].iotlb_id = 1; + test_err_hwpt_invalidate(EOPNOTSUPP, nested_hwpt_id[0], inv_reqs, + IOMMU_HWPT_INVALIDATE_DATA_SELFTEST, + sizeof(*inv_reqs), &num_inv); + assert(num_inv == 1); + test_cmd_hwpt_check_iotlb(nested_hwpt_id[0], 0, 0); + test_cmd_hwpt_check_iotlb(nested_hwpt_id[0], 1, + IOMMU_TEST_IOTLB_DEFAULT); + test_cmd_hwpt_check_iotlb(nested_hwpt_id[0], 2, + IOMMU_TEST_IOTLB_DEFAULT); + test_cmd_hwpt_check_iotlb(nested_hwpt_id[0], 3, + IOMMU_TEST_IOTLB_DEFAULT); + + /* + * Invalidate the 1st iotlb entry but fail the 2nd request + * due to invalid iotlb_id configuration in the 2nd request. + */ + num_inv = 2; + inv_reqs[0].flags = 0; + inv_reqs[0].iotlb_id = 0; + inv_reqs[1].flags = 0; + inv_reqs[1].iotlb_id = MOCK_NESTED_DOMAIN_IOTLB_ID_MAX + 1; + test_err_hwpt_invalidate(EINVAL, nested_hwpt_id[0], inv_reqs, + IOMMU_HWPT_INVALIDATE_DATA_SELFTEST, + sizeof(*inv_reqs), &num_inv); + assert(num_inv == 1); + test_cmd_hwpt_check_iotlb(nested_hwpt_id[0], 0, 0); + test_cmd_hwpt_check_iotlb(nested_hwpt_id[0], 1, + IOMMU_TEST_IOTLB_DEFAULT); + test_cmd_hwpt_check_iotlb(nested_hwpt_id[0], 2, + IOMMU_TEST_IOTLB_DEFAULT); + test_cmd_hwpt_check_iotlb(nested_hwpt_id[0], 3, + IOMMU_TEST_IOTLB_DEFAULT); + + /* Invalidate the 2nd iotlb entry and verify */ + num_inv = 1; + inv_reqs[0].flags = 0; + inv_reqs[0].iotlb_id = 1; + test_cmd_hwpt_invalidate(nested_hwpt_id[0], inv_reqs, + IOMMU_HWPT_INVALIDATE_DATA_SELFTEST, + sizeof(*inv_reqs), &num_inv); + assert(num_inv == 1); + test_cmd_hwpt_check_iotlb(nested_hwpt_id[0], 0, 0); + test_cmd_hwpt_check_iotlb(nested_hwpt_id[0], 1, 0); + test_cmd_hwpt_check_iotlb(nested_hwpt_id[0], 2, + IOMMU_TEST_IOTLB_DEFAULT); + test_cmd_hwpt_check_iotlb(nested_hwpt_id[0], 3, + IOMMU_TEST_IOTLB_DEFAULT); + + /* Invalidate the 3rd and 4th iotlb entries and verify */ + num_inv = 2; + inv_reqs[0].flags = 0; + inv_reqs[0].iotlb_id = 2; + inv_reqs[1].flags = 0; + inv_reqs[1].iotlb_id = 3; + test_cmd_hwpt_invalidate(nested_hwpt_id[0], inv_reqs, + IOMMU_HWPT_INVALIDATE_DATA_SELFTEST, + sizeof(*inv_reqs), &num_inv); + assert(num_inv == 2); + test_cmd_hwpt_check_iotlb_all(nested_hwpt_id[0], 0); + + /* Invalidate all iotlb entries for nested_hwpt_id[1] and verify */ + num_inv = 1; + inv_reqs[0].flags = IOMMU_TEST_INVALIDATE_FLAG_ALL; + test_cmd_hwpt_invalidate(nested_hwpt_id[1], inv_reqs, + IOMMU_HWPT_INVALIDATE_DATA_SELFTEST, + sizeof(*inv_reqs), &num_inv); + assert(num_inv == 1); + test_cmd_hwpt_check_iotlb_all(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]); EXPECT_ERRNO(EBUSY, diff --git a/tools/testing/selftests/iommu/iommufd_utils.h b/tools/testing/selftests/iommu/iommufd_utils.h index fe0a0f566b67..7f41fb796a8a 100644 --- a/tools/testing/selftests/iommu/iommufd_utils.h +++ b/tools/testing/selftests/iommu/iommufd_utils.h @@ -219,6 +219,39 @@ static int _test_cmd_hwpt_alloc(int fd, __u32 device_id, __u32 pt_id, test_cmd_hwpt_check_iotlb(hwpt_id, i, expected); \ }) +static int _test_cmd_hwpt_invalidate(int fd, __u32 hwpt_id, void *reqs, + uint32_t data_type, uint32_t lreq, + uint32_t *nreqs) +{ + struct iommu_hwpt_invalidate cmd = { + .size = sizeof(cmd), + .hwpt_id = hwpt_id, + .data_type = data_type, + .data_uptr = (uint64_t)reqs, + .entry_len = lreq, + .entry_num = *nreqs, + }; + int rc = ioctl(fd, IOMMU_HWPT_INVALIDATE, &cmd); + *nreqs = cmd.entry_num; + return rc; +} + +#define test_cmd_hwpt_invalidate(hwpt_id, reqs, data_type, lreq, nreqs) \ + ({ \ + ASSERT_EQ(0, \ + _test_cmd_hwpt_invalidate(self->fd, hwpt_id, reqs, \ + data_type, \ + lreq, nreqs)); \ + }) +#define test_err_hwpt_invalidate(_errno, hwpt_id, reqs, data_type, lreq, \ + nreqs) \ + ({ \ + EXPECT_ERRNO(_errno, \ + _test_cmd_hwpt_invalidate(self->fd, hwpt_id, \ + reqs, data_type, \ + lreq, nreqs)); \ + }) + static int _test_cmd_access_replace_ioas(int fd, __u32 access_id, unsigned int ioas_id) { From patchwork Thu Jan 11 04:10:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 187165 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2411:b0:101:2151:f287 with SMTP id m17csp1222047dyi; Wed, 10 Jan 2024 20:21:21 -0800 (PST) X-Google-Smtp-Source: AGHT+IGYd3GcIUZ0zso618GUbiyPS50pEfAo+nZn7EWR6MwX8uvyAnKsiGYVNx8txOUjH4kN9T8H X-Received: by 2002:a05:6808:648a:b0:3bc:2ec7:a400 with SMTP id fh10-20020a056808648a00b003bc2ec7a400mr848213oib.102.1704946881632; Wed, 10 Jan 2024 20:21:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704946881; cv=none; d=google.com; s=arc-20160816; b=OfgCXrYXONFZnd+xQk6BegFJHyh2QUK2UVug5WIA1w3E/24wpYKqKwovGEUpa9IZVu Q1KvRlNBYh1Pl0qrYmoPtoYf9p6XsZEw51EFZTPQdH9wgQcWOj5N/Fap2JCQpcfTS6Dv sxw5ThWSlO6M0WBHKm/JzDHQ+1VRE9DScgmtHVwlDBi0VBeO0ap+T1JhwidJzamy48ZG HjM3prufAMPnJEYUBmx/uTNMTzNs1ZLFOMS0IBw+x/2dduhdlHqki2flTt8SBhBS0xRa XHAvW+okAhIz35FXrWB+HWZtZXDIaooVkyck8F2ENVpV9INt1xYlTxwu1WEdbX5i1lfc 4OHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=q4uWv8AV6KNV4SiyAo+kyDNISIEYNPRTu2CU1wX8wto=; fh=aa7ajXSXkJe0gRz4CDJDDWs+jFd1z8IECXwIf+VZeM8=; b=gaZjoBpVxjajvv5DMOnio3vWtIZalETjnZaQS1xeTPhZdN+RCQ3N1FzPZB377Scy5r jvuvAxAo46FeSq0SPtPvkDdWmnjesfXXILAokofI6KKhuF6CTOe4GyIHsExXjDuBW6Su K/vwkB54jBwM0GMJKIZtDJLfzoKQqw0+3YK5feKdzs4Q614sRkoBBcQ//bEPQkt4Gaye N37NGF6WzGgZCTFAZBD7ADNjozZ+loARkTgT/f5CNe24QUFD6CAJS5WhDyH8q2er2ai6 yRXGWNHeXn05p62ZV4ZUDg8FkYZwzz5oCh5rC4NqJ986wgQxz0vJ0gCxmuAtT6Jxx31t V8Vw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=CqhF6760; spf=pass (google.com: domain of linux-kernel+bounces-23054-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-23054-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id a24-20020a056a001d1800b006da4d96cb0asi219470pfx.191.2024.01.10.20.21.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 20:21:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-23054-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=CqhF6760; spf=pass (google.com: domain of linux-kernel+bounces-23054-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-23054-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id C4ED928B6A3 for ; Thu, 11 Jan 2024 04:13:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3BB79FBF1; Thu, 11 Jan 2024 04:10:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="CqhF6760" Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.20]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2F29DD304; Thu, 11 Jan 2024 04:10:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1704946233; x=1736482233; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=fC5z37HXi07mvbopXKTyjsrDjk4TmLvNb4c2PaNCrJ4=; b=CqhF6760gBcLRxJFAsL5vzdG1tk5RkeAagwIaTUD6QkAd48c6SZjjc7Z WsuQd/9D/XNlB3k22ftQnEAtXc5sa22CkcyxJ9WBWBAtR6UnMIAwT/fUy bHVuGzyVtAYijTNQE6toEDX5IivVTx+A8aToGDk5fpHV8s5ndd7ivGX9v NRUPKlfexGgaCT2r4sRe65D4M9LYKGBd7EVRVRee384kcjMF/O8GxVAlw VLl326l2Zmjx9PkRDemKWIcMuEWq4f6WHdarSkUBfFiTyK0R6XPJU4+4c 6LJFsV15xlvkECxi65CvE8J6MpcuriUegDuuP+lt50DHd5TVIGmpnMgUV Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10949"; a="389167690" X-IronPort-AV: E=Sophos;i="6.04,185,1695711600"; d="scan'208";a="389167690" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jan 2024 20:10:32 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10949"; a="1113691901" X-IronPort-AV: E=Sophos;i="6.04,185,1695711600"; d="scan'208";a="1113691901" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga005.fm.intel.com with ESMTP; 10 Jan 2024 20:10:31 -0800 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com, joao.m.martins@oracle.com, xin.zeng@intel.com, yan.y.zhao@intel.com, j.granados@samsung.com, binbin.wu@linux.intel.com Subject: [PATCH v11 7/8] iommufd: Add data structure for Intel VT-d stage-1 cache invalidation Date: Wed, 10 Jan 2024 20:10:14 -0800 Message-Id: <20240111041015.47920-8-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240111041015.47920-1-yi.l.liu@intel.com> References: <20240111041015.47920-1-yi.l.liu@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787766381546561692 X-GMAIL-MSGID: 1787766381546561692 This adds the data structure invalidating caches for the nested domain allocated with IOMMU_HWPT_DATA_VTD_S1 type. Reviewed-by: Kevin Tian Signed-off-by: Lu Baolu Signed-off-by: Yi Liu --- include/uapi/linux/iommufd.h | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index 824560c50ec6..1dfeaa2e649e 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -623,6 +623,42 @@ enum iommu_hwpt_invalidate_data_type { IOMMU_HWPT_INVALIDATE_DATA_VTD_S1, }; +/** + * enum iommu_hwpt_vtd_s1_invalidate_flags - Flags for Intel VT-d + * stage-1 cache invalidation + * @IOMMU_VTD_INV_FLAGS_LEAF: Indicates whether the invalidation applies + * to all-levels page structure cache or just + * the leaf PTE cache. + */ +enum iommu_hwpt_vtd_s1_invalidate_flags { + IOMMU_VTD_INV_FLAGS_LEAF = 1 << 0, +}; + +/** + * struct iommu_hwpt_vtd_s1_invalidate - Intel VT-d cache invalidation + * (IOMMU_HWPT_INVALIDATE_DATA_VTD_S1) + * @addr: The start address of the range to be invalidated. It needs to + * be 4KB aligned. + * @npages: Number of contiguous 4K pages to be invalidated. + * @flags: Combination of enum iommu_hwpt_vtd_s1_invalidate_flags + * @__reserved: Must be 0 + * + * The Intel VT-d specific invalidation data for user-managed stage-1 cache + * invalidation in nested translation. Userspace uses this structure to + * tell the impacted cache scope after modifying the stage-1 page table. + * + * Invalidating all the caches related to the page table by setting @addr + * to be 0 and @npages to be U64_MAX. + * + * The device TLB will be invalidated automatically if ATS is enabled. + */ +struct iommu_hwpt_vtd_s1_invalidate { + __aligned_u64 addr; + __aligned_u64 npages; + __u32 flags; + __u32 __reserved; +}; + /** * struct iommu_hwpt_invalidate - ioctl(IOMMU_HWPT_INVALIDATE) * @size: sizeof(struct iommu_hwpt_invalidate) From patchwork Thu Jan 11 04:10:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 187162 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2411:b0:101:2151:f287 with SMTP id m17csp1219718dyi; Wed, 10 Jan 2024 20:13:28 -0800 (PST) X-Google-Smtp-Source: AGHT+IEqDNro3yss46bE2FNv6uCP3HYcPtbPlfvTgwBwDyM/l3eE+3urPN5bZGhFsPsvnZfJ8ST8 X-Received: by 2002:a05:622a:c8:b0:429:bafc:ed3d with SMTP id p8-20020a05622a00c800b00429bafced3dmr83336qtw.19.1704946407863; Wed, 10 Jan 2024 20:13:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704946407; cv=none; d=google.com; s=arc-20160816; b=Y0kjvG+GZoO+a8yHCzBtOnc8mHdSkzy4YcFXpwV7ntmyrrUmzdrJJlmg5dshRvNLwf EbODrfJuU/FNvJrPcGTn39dXK0PTIi2mdkWXo54c83QYo+a/Z7slMJvIfLtrXBkygE2Q dxPHOsmXnhtalrdSYSK3sdYZID2w/F7ALKtcwBgiE5LfSXjZ7cb7m7ScyJOuuYMr7pE5 uj5lqsoaOMAlCGTB5FJnWeot0HUkqj+xHNn+TXA1eyhIzCUM22D3DScSN8DgBQkf+mHe o26+pdVIwpX3YFd0dpqVZgyutVqNMuC3ro+tywr1anWxbvzoRy0tel4UHlzSzDtR1+BX rH5w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=CzPAMvTrUGUPJZq8oLQ/EdvlbzClHAMsEqXoZuxGnoo=; fh=aa7ajXSXkJe0gRz4CDJDDWs+jFd1z8IECXwIf+VZeM8=; b=Crfj8idwQtPsZMA+U4zQn8iv+BcfAHnf007tHy8EferkxYYcn0PC1zCJED/vbihoC4 VVSVashHv8hBLteTLV6L3ntOg0pAWNT7z5tpF+LykhDFXu1ls1VoZxrormTOSkqJOhZr nAmF5IFZVL45zqGdGPicZ9fwuLpYGz6kjkOR5Mf3hD+onXvbMnczhWKuCkoyldw2DN/4 buzJr1b4AMMxr7Y8LFNhS6hTKY1zybgUb8/Mthzb5vCDWS0z0TmHa0MhSBstgUu9w5ar B6JrC8UXYLsn3+KFmnT92Tve+nRi2Tuw9+monJC80FbMluEEejv1TBY1ByLTxqVO38YQ ypfw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=asQJoT0o; spf=pass (google.com: domain of linux-kernel+bounces-23056-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-23056-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id x10-20020a05622a000a00b00429c482b5f1si230042qtw.671.2024.01.10.20.13.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 20:13:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-23056-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=asQJoT0o; spf=pass (google.com: domain of linux-kernel+bounces-23056-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-23056-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id A63071C23281 for ; Thu, 11 Jan 2024 04:13:27 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DE8C7101C3; Thu, 11 Jan 2024 04:10:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="asQJoT0o" Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.20]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C117ADF69; Thu, 11 Jan 2024 04:10:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1704946234; x=1736482234; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=sjL5Dfrqx7fX931BlScUsmc+6VABv8SzlS11acNijUI=; b=asQJoT0oeFC4yI2fXJKJ0sZW3UljPh8MAM5s2Fkw00Pq87Ve3NDOGndd SdtJycS7sx/GZfQsg/smnOF79ZxNV6apITivBFYkItyvGctxd3eGtyN2M 5DDT3Um4e9h3i5/tl0vIGr9YG2strzIW3i/f+n9DcTcd5IE6JDemmoBmp 1oX/Ed5a9v4sLiPOozPZNwJeaNttSrzuJ3mC+HIdy+h5HwGIDL00gCFNq MpxHkBH2T4lKcJ3w6tNZ19virCUgqmsM0PUAGnfKXAjEXUACZSO9Rcun2 oHMjX6Cu6JFxRPByyA/Sp6T8z2Kbhy1fonvd/f/AaJkFjx/fvSfae7qHa g==; X-IronPort-AV: E=McAfee;i="6600,9927,10949"; a="389167702" X-IronPort-AV: E=Sophos;i="6.04,185,1695711600"; d="scan'208";a="389167702" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jan 2024 20:10:34 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10949"; a="1113691918" X-IronPort-AV: E=Sophos;i="6.04,185,1695711600"; d="scan'208";a="1113691918" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga005.fm.intel.com with ESMTP; 10 Jan 2024 20:10:32 -0800 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com, joao.m.martins@oracle.com, xin.zeng@intel.com, yan.y.zhao@intel.com, j.granados@samsung.com, binbin.wu@linux.intel.com Subject: [PATCH v11 8/8] iommu/vt-d: Add iotlb flush for nested domain Date: Wed, 10 Jan 2024 20:10:15 -0800 Message-Id: <20240111041015.47920-9-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240111041015.47920-1-yi.l.liu@intel.com> References: <20240111041015.47920-1-yi.l.liu@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787765884458195354 X-GMAIL-MSGID: 1787765884458195354 From: Lu Baolu This implements the .cache_invalidate_user() callback to support iotlb flush for nested domain. Reviewed-by: Kevin Tian Signed-off-by: Lu Baolu Co-developed-by: Yi Liu Signed-off-by: Yi Liu --- drivers/iommu/intel/nested.c | 88 ++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/drivers/iommu/intel/nested.c b/drivers/iommu/intel/nested.c index b5a5563ab32c..1e866ed02fef 100644 --- a/drivers/iommu/intel/nested.c +++ b/drivers/iommu/intel/nested.c @@ -73,9 +73,97 @@ static void intel_nested_domain_free(struct iommu_domain *domain) kfree(to_dmar_domain(domain)); } +static void nested_flush_dev_iotlb(struct dmar_domain *domain, u64 addr, + unsigned mask) +{ + struct device_domain_info *info; + unsigned long flags; + u16 sid, qdep; + + spin_lock_irqsave(&domain->lock, flags); + list_for_each_entry(info, &domain->devices, link) { + if (!info->ats_enabled) + continue; + sid = info->bus << 8 | info->devfn; + qdep = info->ats_qdep; + qi_flush_dev_iotlb(info->iommu, sid, info->pfsid, + qdep, addr, mask); + quirk_extra_dev_tlb_flush(info, addr, mask, + IOMMU_NO_PASID, qdep); + } + spin_unlock_irqrestore(&domain->lock, flags); +} + +static void intel_nested_flush_cache(struct dmar_domain *domain, u64 addr, + unsigned long npages, bool ih) +{ + struct iommu_domain_info *info; + unsigned long i; + unsigned mask; + + xa_for_each(&domain->iommu_array, i, info) + qi_flush_piotlb(info->iommu, + domain_id_iommu(domain, info->iommu), + IOMMU_NO_PASID, addr, npages, ih); + + if (!domain->has_iotlb_device) + return; + + if (npages == U64_MAX) + mask = 64 - VTD_PAGE_SHIFT; + else + mask = ilog2(__roundup_pow_of_two(npages)); + + nested_flush_dev_iotlb(domain, addr, mask); +} + +static int intel_nested_cache_invalidate_user(struct iommu_domain *domain, + struct iommu_user_data_array *array) +{ + struct dmar_domain *dmar_domain = to_dmar_domain(domain); + struct iommu_hwpt_vtd_s1_invalidate inv_entry; + u32 index, processed = 0; + int ret = 0; + + if (array->type != IOMMU_HWPT_INVALIDATE_DATA_VTD_S1) { + ret = -EINVAL; + goto out; + } + + for (index = 0; index < array->entry_num; index++) { + ret = iommu_copy_struct_from_user_array(&inv_entry, array, + IOMMU_HWPT_INVALIDATE_DATA_VTD_S1, + index, __reserved); + if (ret) + break; + + if ((inv_entry.flags & ~IOMMU_VTD_INV_FLAGS_LEAF) || + inv_entry.__reserved) { + ret = -EOPNOTSUPP; + break; + } + + if (!IS_ALIGNED(inv_entry.addr, VTD_PAGE_SIZE) || + ((inv_entry.npages == U64_MAX) && inv_entry.addr)) { + ret = -EINVAL; + break; + } + + intel_nested_flush_cache(dmar_domain, inv_entry.addr, + inv_entry.npages, + inv_entry.flags & IOMMU_VTD_INV_FLAGS_LEAF); + processed++; + } + +out: + array->entry_num = processed; + return ret; +} + static const struct iommu_domain_ops intel_nested_domain_ops = { .attach_dev = intel_nested_attach_dev, .free = intel_nested_domain_free, + .cache_invalidate_user = intel_nested_cache_invalidate_user, }; struct iommu_domain *intel_nested_domain_alloc(struct iommu_domain *parent,