From nobody Sat Feb 7 21:05:36 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 CF953EB64D9 for ; Tue, 11 Jul 2023 01:09:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230375AbjGKBJK (ORCPT ); Mon, 10 Jul 2023 21:09:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50912 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231142AbjGKBJG (ORCPT ); Mon, 10 Jul 2023 21:09:06 -0400 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D0D9D1BC; Mon, 10 Jul 2023 18:08:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1689037735; x=1720573735; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=HDO/3SJrjs+J31UeSjn79Qj5Ue9JvEOiFH2FjTHY2ts=; b=i4Pl+QTkVVwXGp4WyejyWSMcw82NkZBlcgEKRyidW5J+B+Q9ki8JIbpU BhOoQlgSj7W3BtT6Aso7QCEkZFaokUi25glkOn7U7tZRESTdRsM1aMbj5 ItG4KFk/QbzsmKivPVVUWd9ODNHtcu60fBDxVxICBgYy0i38wnULl6gFM lz71j3YNpN6uHM7bVsHhuhZU2pgri5PBqHmYnMTlS5rdu0uS0faIO3csO VzTzozOMDnHQf7EtOQykwc2eKx4rc2HhRUXMEH3RvAabv8HTEKiL8/yBW dt/jClzZPasPoA85VP9xpFIoFICKVzoD5Zwf9HVKUDgTzATSm8TmqUxyA w==; X-IronPort-AV: E=McAfee;i="6600,9927,10767"; a="344816109" X-IronPort-AV: E=Sophos;i="6.01,195,1684825200"; d="scan'208";a="344816109" 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:08:55 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10767"; a="810999890" X-IronPort-AV: E=Sophos;i="6.01,195,1684825200"; d="scan'208";a="810999890" Received: from allen-box.sh.intel.com ([10.239.159.127]) by FMSMGA003.fm.intel.com with ESMTP; 10 Jul 2023 18:08:51 -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 5/9] iommu: Make fault_param generic Date: Tue, 11 Jul 2023 09:06:38 +0800 Message-Id: <20230711010642.19707-6-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" The iommu faults, including recoverable faults (IO page faults) and unrecoverable faults (DMA faults), are generic to all devices. The iommu faults could possibly be triggered for every device. The fault_param pointer under struct dev_iommu is the per-device fault data. Therefore, the fault_param pointer should be allocated during iommu device probe and freed when the device is released. With this done, the individual iommu drivers that support iopf have no need to call iommu_[un]register_device_fault_handler() any more. This will make it easier for the iommu drivers to support iopf, and it will also make the fault_param allocation and free simpler. Signed-off-by: Lu Baolu --- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 13 +------------ drivers/iommu/intel/iommu.c | 18 ++++-------------- drivers/iommu/iommu.c | 14 ++++++++++++++ 3 files changed, 19 insertions(+), 26 deletions(-) diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c b/drivers/iomm= u/arm/arm-smmu-v3/arm-smmu-v3-sva.c index a5a63b1c947e..fa8ab9d413f8 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c @@ -437,7 +437,6 @@ bool arm_smmu_master_sva_enabled(struct arm_smmu_master= *master) =20 static int arm_smmu_master_sva_enable_iopf(struct arm_smmu_master *master) { - int ret; struct device *dev =3D master->dev; =20 /* @@ -450,16 +449,7 @@ static int arm_smmu_master_sva_enable_iopf(struct arm_= smmu_master *master) if (!master->iopf_enabled) return -EINVAL; =20 - ret =3D iopf_queue_add_device(master->smmu->evtq.iopf, dev); - if (ret) - return ret; - - ret =3D iommu_register_device_fault_handler(dev, iommu_queue_iopf, dev); - if (ret) { - iopf_queue_remove_device(master->smmu->evtq.iopf, dev); - return ret; - } - return 0; + return iopf_queue_add_device(master->smmu->evtq.iopf, dev); } =20 static void arm_smmu_master_sva_disable_iopf(struct arm_smmu_master *maste= r) @@ -469,7 +459,6 @@ static void arm_smmu_master_sva_disable_iopf(struct arm= _smmu_master *master) if (!master->iopf_enabled) return; =20 - iommu_unregister_device_fault_handler(dev); iopf_queue_remove_device(master->smmu->evtq.iopf, dev); } =20 diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index 5c8c5cdc36cf..22e43db20252 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -4594,23 +4594,14 @@ static int intel_iommu_enable_iopf(struct device *d= ev) if (ret) return ret; =20 - ret =3D iommu_register_device_fault_handler(dev, iommu_queue_iopf, dev); - if (ret) - goto iopf_remove_device; - ret =3D pci_enable_pri(pdev, PRQ_DEPTH); - if (ret) - goto iopf_unregister_handler; + if (ret) { + iopf_queue_remove_device(iommu->iopf_queue, dev); + return ret; + } info->pri_enabled =3D 1; =20 return 0; - -iopf_unregister_handler: - iommu_unregister_device_fault_handler(dev); -iopf_remove_device: - iopf_queue_remove_device(iommu->iopf_queue, dev); - - return ret; } =20 static int intel_iommu_disable_iopf(struct device *dev) @@ -4637,7 +4628,6 @@ static int intel_iommu_disable_iopf(struct device *de= v) * fault handler and removing device from iopf queue should never * fail. */ - WARN_ON(iommu_unregister_device_fault_handler(dev)); WARN_ON(iopf_queue_remove_device(iommu->iopf_queue, dev)); =20 return 0; diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 65895b987e22..8d1f0935ea71 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -299,7 +299,15 @@ static int dev_iommu_get(struct device *dev) return -ENOMEM; =20 mutex_init(¶m->lock); + param->fault_param =3D kzalloc(sizeof(*param->fault_param), GFP_KERNEL); + if (!param->fault_param) { + kfree(param); + return -ENOMEM; + } + mutex_init(¶m->fault_param->lock); + INIT_LIST_HEAD(¶m->fault_param->faults); dev->iommu =3D param; + return 0; } =20 @@ -312,6 +320,12 @@ static void dev_iommu_free(struct device *dev) fwnode_handle_put(param->fwspec->iommu_fwnode); kfree(param->fwspec); } + /* + * All pending faults should have been drained before + * device release. + */ + WARN_ON_ONCE(!list_empty(¶m->fault_param->faults)); + kfree(param->fault_param); kfree(param); } =20 --=20 2.34.1