Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp36103666rwd; Mon, 10 Jul 2023 18:12:56 -0700 (PDT) X-Google-Smtp-Source: APBJJlFw87MYWuOX/zsq/cqm4sSArCquv/wAGh09Str9ZlszvwcNF8JLIcLeH/mlk24y4K5/IU2j X-Received: by 2002:a17:906:c30e:b0:992:5135:4113 with SMTP id s14-20020a170906c30e00b0099251354113mr10267000ejz.37.1689037976085; Mon, 10 Jul 2023 18:12:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689037976; cv=none; d=google.com; s=arc-20160816; b=QaLGDhf2p2Exhnw3Kt/kBq1mJlKe3gjdDC0oMbhP0YyDE8etK5MR94jZMUUoTMhvRM m2CUFXhBj1AM2uM6DF1oLnQRtA7zvtk+rZ1BiwDI7NZ+IQPg4o4933gPFgrnH14LnAnE NEhwPGVZKnVDLnZCFe/Z0CiXkKVo0j1jR5WTikRQ8KFXZvpGxJbk7qWzrK84LF+JzK2q 80AB0k3Vx2cQIhLfNZjJOPNgRKYyovvifoJ7gbX9WUeFcKwqhi4ZQdmMjamsrMiuURHH Nmotc4H73qAOe33te/zUWmj7wOJ70sKejcIDlAdw8coMSiMKoHVzZaUXjzDjolbfod2a 7O+A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=3O1DPerNaIuglT5EO1TVnObXCXHkAOrSpjCriD4NRWA=; fh=5v0QJkujOnxKSzhmeWiMX/0q9X35NwMz+SRMkKa4moc=; b=Tkyokd1Ll9x7A1IVosldqBSOjvgvOOP5nZ8eWcE7cWp8TFbCyPj8Y3auRZiEeCTxF6 yfiaEGW3uMMB6AntunPpFfFfYCWbflHw7o3M+1wHyOWVbzmbLcOd7L/uxKqeAOJnYCCt mbyFhvrVeIsyPIhgZu2uS9KQ6UMuX9H+BmdkqQwP67Mf0i4wKNLI5EtPvkuhXmHd7uPY DavXeUR7LgxeOZleC1shIHUobhi8D2233DywfffXyfOYYt2U1rW1VcxQ2RNtd1DndEiZ bz19HP6yrqcMOxXKmRVrnb6T3SdxG3k0EmHOZE6Pkyr0kxeQ29M9v7lE42ElRF+lDbjS 9TGQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=i4Pl+QTk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e11-20020a1709067e0b00b00992f81122e9si814972ejr.340.2023.07.10.18.12.33; Mon, 10 Jul 2023 18:12:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=i4Pl+QTk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231144AbjGKBJK (ORCPT + 99 others); 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: 8bit X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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/iommu/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) static int arm_smmu_master_sva_enable_iopf(struct arm_smmu_master *master) { - int ret; struct device *dev = master->dev; /* @@ -450,16 +449,7 @@ static int arm_smmu_master_sva_enable_iopf(struct arm_smmu_master *master) if (!master->iopf_enabled) return -EINVAL; - ret = iopf_queue_add_device(master->smmu->evtq.iopf, dev); - if (ret) - return ret; - - ret = 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); } static void arm_smmu_master_sva_disable_iopf(struct arm_smmu_master *master) @@ -469,7 +459,6 @@ static void arm_smmu_master_sva_disable_iopf(struct arm_smmu_master *master) if (!master->iopf_enabled) return; - iommu_unregister_device_fault_handler(dev); iopf_queue_remove_device(master->smmu->evtq.iopf, dev); } 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 *dev) if (ret) return ret; - ret = iommu_register_device_fault_handler(dev, iommu_queue_iopf, dev); - if (ret) - goto iopf_remove_device; - ret = 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 = 1; return 0; - -iopf_unregister_handler: - iommu_unregister_device_fault_handler(dev); -iopf_remove_device: - iopf_queue_remove_device(iommu->iopf_queue, dev); - - return ret; } static int intel_iommu_disable_iopf(struct device *dev) @@ -4637,7 +4628,6 @@ static int intel_iommu_disable_iopf(struct device *dev) * 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)); 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; mutex_init(¶m->lock); + param->fault_param = 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 = param; + return 0; } @@ -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); } -- 2.34.1