From nobody Sat Feb 7 18:52:09 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1DA46EB64DA for ; Tue, 11 Jul 2023 01:09:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231228AbjGKBJh (ORCPT ); Mon, 10 Jul 2023 21:09:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51384 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231137AbjGKBJe (ORCPT ); Mon, 10 Jul 2023 21:09:34 -0400 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E7BC01708; Mon, 10 Jul 2023 18:09:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1689037746; x=1720573746; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=kDNvl2xq4daGW1gpeGo7tFuYd3E44JZ84AN2RiOMGAA=; b=H7jZgCo83x820b1WDmJwyrdQArT6k6hB5aKvVm/ln1+ZFfQbWrXKVIg6 pD3Z06j7zPgxKtTDFLLR4Tr+9TA5xS/rQkHBWwBCNxI11ZMgXYiR3PKN2 6YuGonTOdR3hJQbjyfLPmbx4zNio8TqlM6gkyH93PBMH8BE2wzYFrKMK6 x1OxF9ltQNifACxKe90x1xnLvTlAKwKlozdBLvI/LxbucQ8sFxCGlbDH4 h/yf3QXxRhyHrXq2MShijIkCrQFmZ2N0mfj4KhhS29zhGhIQD7wSoDTI2 iL+1ShhHLS0XXf7ISM5OGvFkbtDvqOYecDIJlAk5WLU/rSXv1Ac8qdbHA w==; X-IronPort-AV: E=McAfee;i="6600,9927,10767"; a="344816151" X-IronPort-AV: E=Sophos;i="6.01,195,1684825200"; d="scan'208";a="344816151" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jul 2023 18:09:06 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10767"; a="810999949" X-IronPort-AV: E=Sophos;i="6.01,195,1684825200"; d="scan'208";a="810999949" Received: from allen-box.sh.intel.com ([10.239.159.127]) by FMSMGA003.fm.intel.com with ESMTP; 10 Jul 2023 18:09:02 -0700 From: Lu Baolu To: Joerg Roedel , Will Deacon , Robin Murphy , Jason Gunthorpe , Kevin Tian , Jean-Philippe Brucker , Nicolin Chen Cc: Yi Liu , Jacob Pan , iommu@lists.linux.dev, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Lu Baolu Subject: [PATCH 8/9] iommu: Add iommu page fault cookie helpers Date: Tue, 11 Jul 2023 09:06:41 +0800 Message-Id: <20230711010642.19707-9-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230711010642.19707-1-baolu.lu@linux.intel.com> References: <20230711010642.19707-1-baolu.lu@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add an xarray in iommu_fault_param as place holder for per-{device, pasid} fault cookie. They could be used in varous cases. For example, SVA needs to handle IO page faults through work queues to batch process all faults in a fault group. It needs a fault cookie as a temporary storage of partial faults, together with other meta data. The iommufd will also use the fault cookie to store the mapping of device object ID and a pasid of a device. This allows the iommufd to quickly retrieve the device object ID for a given {device, pasid} pair in the hot path of IO page fault delivery. Signed-off-by: Lu Baolu --- include/linux/iommu.h | 16 ++++++++++++++++ drivers/iommu/iommu.c | 44 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/include/linux/iommu.h b/include/linux/iommu.h index c86ff10b40f3..ffd6fe1317f4 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -539,10 +539,12 @@ struct iommu_fault_event { * struct iommu_fault_param - per-device IOMMU fault data * @faults: holds the pending faults which needs response * @lock: protect pending faults list + * @pasid_cookie: per-pasid fault cookie */ struct iommu_fault_param { struct list_head faults; struct mutex lock; + struct xarray pasid_cookie; }; =20 /** @@ -636,6 +638,8 @@ extern void iommu_set_fault_handler(struct iommu_domain= *domain, iommu_fault_handler_t handler, void *token); void iommu_domain_set_iopf_handler(struct iommu_domain *domain, iommu_iopf_handler_t handler, void *data); +void *iommu_set_device_fault_cookie(struct device *dev, ioasid_t pasid, vo= id *cookie); +void *iommu_get_device_fault_cookie(struct device *dev, ioasid_t pasid); =20 extern void iommu_get_resv_regions(struct device *dev, struct list_head *l= ist); extern void iommu_put_resv_regions(struct device *dev, struct list_head *l= ist); @@ -965,6 +969,18 @@ static inline void iommu_domain_set_iopf_handler(struc= t iommu_domain *domain, { } =20 +static inline void *iommu_set_device_fault_cookie(struct device *dev, + ioasid_t pasid, void *cookie) +{ + return ERR_PTR(-ENODEV); +} + +static inline void *iommu_get_device_fault_cookie(struct device *dev, + ioasid_t pasid) +{ + return ERR_PTR(-ENODEV); +} + static inline void iommu_get_resv_regions(struct device *dev, struct list_head *list) { diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 3dc59af24208..d52b827982a4 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -1336,6 +1336,50 @@ int iommu_page_response(struct device *dev, } EXPORT_SYMBOL_GPL(iommu_page_response); =20 +/** + * iommu_set_device_fault_cookie - Set a fault cookie for per-{device, pas= id} + * @dev: the device to set the cookie + * @pasid: the pasid on this device + * @cookie: the opaque data + * + * Return the old cookie on success, or ERR_PTR(err#) on failure. + */ +void *iommu_set_device_fault_cookie(struct device *dev, ioasid_t pasid, + void *cookie) +{ + struct iommu_fault_param *fault_param; + void *curr; + + if (!dev->iommu || !dev->iommu->fault_param) + return ERR_PTR(-ENODEV); + + fault_param =3D dev->iommu->fault_param; + curr =3D xa_store(&fault_param->pasid_cookie, pasid, cookie, GFP_KERNEL); + + return xa_is_err(curr) ? ERR_PTR(xa_err(curr)) : curr; +} +EXPORT_SYMBOL_GPL(iommu_set_device_fault_cookie); + +/** + * iommu_get_device_fault_cookie - Get the fault cookie for {device, pasid} + * @dev: the device to set the cookie + * @pasid: the pasid on this device + * + * Return the cookie on success, or ERR_PTR(err#) on failure. + */ +void *iommu_get_device_fault_cookie(struct device *dev, ioasid_t pasid) +{ + struct iommu_fault_param *fault_param; + + if (!dev->iommu || !dev->iommu->fault_param) + return ERR_PTR(-ENODEV); + + fault_param =3D dev->iommu->fault_param; + + return xa_load(&fault_param->pasid_cookie, pasid); +} +EXPORT_SYMBOL_GPL(iommu_get_device_fault_cookie); + /** * iommu_group_id - Return ID for a group * @group: the group to ID --=20 2.34.1