From nobody Fri Dec 19 09:28:11 2025 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 A2DECC4167B for ; Thu, 7 Dec 2023 06:49:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377848AbjLGGtD (ORCPT ); Thu, 7 Dec 2023 01:49:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37630 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231201AbjLGGsl (ORCPT ); Thu, 7 Dec 2023 01:48:41 -0500 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6DA30D5E; Wed, 6 Dec 2023 22:48:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1701931727; x=1733467727; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ofpi/Tax1CbViISBnDZxgxdOWEDrJot3YJzEmpMmJQ8=; b=lLzHj4twYpHHYDoLo2X8KHee1o70SsxsbMhNxiWfmEoQ3+GX5kG3OIsD pujkKdQuGIvch3Pqn9KmR4qjVp4yrOOBLtK7p37p+mk5vP9wUfNKrrycU eJn1hbIaK/ePJSlfE46o4z3xstSfAzp5YURlsqE52JiVynesxXwOcQWyj Hycx7KNm5ENnNb5oAhjSIOJqzoWpLfVmFQZ3NLMFFyJLPjFt+lo170tX3 GfLAFEOxmnRfSOgnNieDzryWk/ftDg2HA+NieIXjJQSYiwKVKAlAmvfTF YX0CGmu2ISa80rU68VgVUSxgUhVoSvC3sDCYb5EGvA8O2YuuBX+WtTh7p Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10916"; a="1015045" X-IronPort-AV: E=Sophos;i="6.04,256,1695711600"; d="scan'208";a="1015045" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Dec 2023 22:48:47 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10916"; a="771611771" X-IronPort-AV: E=Sophos;i="6.04,256,1695711600"; d="scan'208";a="771611771" Received: from allen-box.sh.intel.com ([10.239.159.127]) by orsmga002.jf.intel.com with ESMTP; 06 Dec 2023 22:48:42 -0800 From: Lu Baolu To: Joerg Roedel , Will Deacon , Robin Murphy , Jason Gunthorpe , Kevin Tian , Jean-Philippe Brucker , Nicolin Chen Cc: Yi Liu , Jacob Pan , Longfang Liu , Yan Zhao , iommu@lists.linux.dev, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Lu Baolu , Jason Gunthorpe Subject: [PATCH v8 09/12] iommu: Make iommu_queue_iopf() more generic Date: Thu, 7 Dec 2023 14:43:05 +0800 Message-Id: <20231207064308.313316-10-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231207064308.313316-1-baolu.lu@linux.intel.com> References: <20231207064308.313316-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" Make iommu_queue_iopf() more generic by making the iopf_group a minimal set of iopf's that an iopf handler of domain should handle and respond to. Add domain parameter to struct iopf_group so that the handler can retrieve and use it directly. Change iommu_queue_iopf() to forward groups of iopf's to the domain's iopf handler. This is also a necessary step to decouple the sva iopf handling code from this interface. Signed-off-by: Lu Baolu Reviewed-by: Kevin Tian Reviewed-by: Jason Gunthorpe Reviewed-by: Yi Liu Tested-by: Yan Zhao Tested-by: Longfang Liu --- include/linux/iommu.h | 4 +-- drivers/iommu/iommu-sva.h | 6 ++--- drivers/iommu/io-pgfault.c | 55 +++++++++++++++++++++++++++++--------- drivers/iommu/iommu-sva.c | 3 +-- 4 files changed, 48 insertions(+), 20 deletions(-) diff --git a/include/linux/iommu.h b/include/linux/iommu.h index c17a99648e44..dcd22bb381a0 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -130,6 +130,7 @@ struct iopf_group { struct list_head faults; struct work_struct work; struct device *dev; + struct iommu_domain *domain; }; =20 /** @@ -209,8 +210,7 @@ struct iommu_domain { unsigned long pgsize_bitmap; /* Bitmap of page sizes in use */ struct iommu_domain_geometry geometry; struct iommu_dma_cookie *iova_cookie; - enum iommu_page_response_code (*iopf_handler)(struct iommu_fault *fault, - void *data); + int (*iopf_handler)(struct iopf_group *group); void *fault_data; union { struct { diff --git a/drivers/iommu/iommu-sva.h b/drivers/iommu/iommu-sva.h index de7819c796ce..27c8da115b41 100644 --- a/drivers/iommu/iommu-sva.h +++ b/drivers/iommu/iommu-sva.h @@ -22,8 +22,7 @@ int iopf_queue_flush_dev(struct device *dev); struct iopf_queue *iopf_queue_alloc(const char *name); void iopf_queue_free(struct iopf_queue *queue); int iopf_queue_discard_partial(struct iopf_queue *queue); -enum iommu_page_response_code -iommu_sva_handle_iopf(struct iommu_fault *fault, void *data); +int iommu_sva_handle_iopf(struct iopf_group *group); =20 #else /* CONFIG_IOMMU_SVA */ static inline int iommu_queue_iopf(struct iommu_fault *fault, struct devic= e *dev) @@ -62,8 +61,7 @@ static inline int iopf_queue_discard_partial(struct iopf_= queue *queue) return -ENODEV; } =20 -static inline enum iommu_page_response_code -iommu_sva_handle_iopf(struct iommu_fault *fault, void *data) +static inline int iommu_sva_handle_iopf(struct iopf_group *group) { return IOMMU_PAGE_RESP_INVALID; } diff --git a/drivers/iommu/io-pgfault.c b/drivers/iommu/io-pgfault.c index c7e6bbed5c05..ccd0e5fefe1d 100644 --- a/drivers/iommu/io-pgfault.c +++ b/drivers/iommu/io-pgfault.c @@ -13,6 +13,9 @@ =20 #include "iommu-sva.h" =20 +enum iommu_page_response_code +iommu_sva_handle_mm(struct iommu_fault *fault, struct mm_struct *mm); + static void iopf_free_group(struct iopf_group *group) { struct iopf_fault *iopf, *next; @@ -45,23 +48,18 @@ static void iopf_handler(struct work_struct *work) { struct iopf_fault *iopf; struct iopf_group *group; - struct iommu_domain *domain; enum iommu_page_response_code status =3D IOMMU_PAGE_RESP_SUCCESS; =20 group =3D container_of(work, struct iopf_group, work); - domain =3D iommu_get_domain_for_dev_pasid(group->dev, - group->last_fault.fault.prm.pasid, 0); - if (!domain || !domain->iopf_handler) - status =3D IOMMU_PAGE_RESP_INVALID; - list_for_each_entry(iopf, &group->faults, list) { /* * For the moment, errors are sticky: don't handle subsequent * faults in the group if there is an error. */ - if (status =3D=3D IOMMU_PAGE_RESP_SUCCESS) - status =3D domain->iopf_handler(&iopf->fault, - domain->fault_data); + if (status !=3D IOMMU_PAGE_RESP_SUCCESS) + break; + + status =3D iommu_sva_handle_mm(&iopf->fault, group->domain->mm); } =20 iopf_complete_group(group->dev, &group->last_fault, status); @@ -113,6 +111,7 @@ int iommu_queue_iopf(struct iommu_fault *fault, struct = device *dev) int ret; struct iopf_group *group; struct iopf_fault *iopf, *next; + struct iommu_domain *domain =3D NULL; struct iommu_fault_param *iopf_param; struct dev_iommu *param =3D dev->iommu; =20 @@ -143,6 +142,23 @@ int iommu_queue_iopf(struct iommu_fault *fault, struct= device *dev) return 0; } =20 + if (fault->prm.flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID) { + domain =3D iommu_get_domain_for_dev_pasid(dev, fault->prm.pasid, 0); + if (IS_ERR(domain)) + domain =3D NULL; + } + + if (!domain) + domain =3D iommu_get_domain_for_dev(dev); + + if (!domain || !domain->iopf_handler) { + dev_warn_ratelimited(dev, + "iopf (pasid %d) without domain attached or handler installed\n", + fault->prm.pasid); + ret =3D -ENODEV; + goto cleanup_partial; + } + group =3D kzalloc(sizeof(*group), GFP_KERNEL); if (!group) { /* @@ -157,8 +173,8 @@ int iommu_queue_iopf(struct iommu_fault *fault, struct = device *dev) group->dev =3D dev; group->last_fault.fault =3D *fault; INIT_LIST_HEAD(&group->faults); + group->domain =3D domain; list_add(&group->last_fault.list, &group->faults); - INIT_WORK(&group->work, iopf_handler); =20 /* See if we have partial faults for this group */ list_for_each_entry_safe(iopf, next, &iopf_param->partial, list) { @@ -167,9 +183,13 @@ int iommu_queue_iopf(struct iommu_fault *fault, struct= device *dev) list_move(&iopf->list, &group->faults); } =20 - queue_work(iopf_param->queue->wq, &group->work); - return 0; + mutex_unlock(&iopf_param->lock); + ret =3D domain->iopf_handler(group); + mutex_lock(&iopf_param->lock); + if (ret) + iopf_free_group(group); =20 + return ret; cleanup_partial: list_for_each_entry_safe(iopf, next, &iopf_param->partial, list) { if (iopf->fault.prm.grpid =3D=3D fault->prm.grpid) { @@ -181,6 +201,17 @@ int iommu_queue_iopf(struct iommu_fault *fault, struct= device *dev) } EXPORT_SYMBOL_GPL(iommu_queue_iopf); =20 +int iommu_sva_handle_iopf(struct iopf_group *group) +{ + struct iommu_fault_param *fault_param =3D group->dev->iommu->fault_param; + + INIT_WORK(&group->work, iopf_handler); + if (!queue_work(fault_param->queue->wq, &group->work)) + return -EBUSY; + + return 0; +} + /** * iopf_queue_flush_dev - Ensure that all queued faults have been processed * @dev: the endpoint whose faults need to be flushed. diff --git a/drivers/iommu/iommu-sva.c b/drivers/iommu/iommu-sva.c index b78671a8a914..ba0d5b7e106a 100644 --- a/drivers/iommu/iommu-sva.c +++ b/drivers/iommu/iommu-sva.c @@ -149,11 +149,10 @@ EXPORT_SYMBOL_GPL(iommu_sva_get_pasid); * I/O page fault handler for SVA */ enum iommu_page_response_code -iommu_sva_handle_iopf(struct iommu_fault *fault, void *data) +iommu_sva_handle_mm(struct iommu_fault *fault, struct mm_struct *mm) { vm_fault_t ret; struct vm_area_struct *vma; - struct mm_struct *mm =3D data; unsigned int access_flags =3D 0; unsigned int fault_flags =3D FAULT_FLAG_REMOTE; struct iommu_fault_page_request *prm =3D &fault->prm; --=20 2.34.1